最近在做一个项目,要求android从sqlserver数据库接受数据。已经成功把数据发布到网上,从网上找到相关代码,也能稍微理解一下,但是为什么就是接受不起来呢
不明白到底具体android是如何接受web数据的
下面是代码
package com.example.test2;
import android.app.Activity;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String BASE_URL = "http://localhost:8080/thrid/SQLServerTest";
private TextView No1;
private TextView No2;
private TextView No3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
/**
* 初始化
*/
private void setupViews(){
No1 = (TextView)findViewById(R.id.textView1);
No2 = (TextView)findViewById(R.id.textView2);
No3 = (TextView)findViewById(R.id.textView3);
try {
//获取后台返回的Json对象
JSONObject mJsonObject = JSONUtil.getJSON(BASE_URL);
No1.setText("获取成功!"); //测试
JSONArray mJsonArray = mJsonObject.getJSONArray("student");
JSONObject firstStudent = mJsonArray.getJSONObject(0);
JSONObject secondStudent = mJsonArray.getJSONObject(1);
JSONObject thridStudent = mJsonArray.getJSONObject(2);
int first_id = firstStudent.getInt("id");
String first_name = firstStudent.getString("name");
int first_value = firstStudent.getInt("value");
int second_id = secondStudent.getInt("id");
String second_name = secondStudent.getString("name");
int second_value = secondStudent.getInt("value");
int thrid_id = thridStudent.getInt("id");
String thrid_name = thridStudent.getString("name");
int thrid_value = thridStudent.getInt("value");
No1.setText("id:"+first_id+",name:"+first_name+",value:"+first_value);
No2.setText("id:"+second_id+",name:"+second_name+",value:"+second_value);
No3.setText("id:"+thrid_id+",name:"+thrid_name+",value:"+thrid_value);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.test2;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
/**
Json封装的工具类.
*/
public class JSONUtil {
private static final String TAG = "JSONUtil";
/**
@throws ConnectionException
*/
public static JSONObject getJSON(String url) throws JSONException, Exception {
return new JSONObject(getRequest(url));
}
/**
向api发送get请求,返回从后台取得的信息。
@param url
@return String
*/
protected static String getRequest(String url) throws Exception {
return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));
}
/**
向api发送get请求,返回从后台取得的信息。
@param url
@param client
@return String
*/
protected static String getRequest(String url, DefaultHttpClient client) throws Exception {
String result = null;
int statusCode = 0;
HttpGet getMethod = new HttpGet(url);
Log.d(TAG, "do the getRequest,url="+url+"");
try {
//getMethod.setHeader("User-Agent", USER_AGENT);
HttpResponse httpResponse = client.execute(getMethod);
//statusCode == 200 正常
statusCode = httpResponse.getStatusLine().getStatusCode();
Log.d(TAG, "statuscode = "+statusCode);
//处理返回的httpResponse信息
result = retrieveInputStream(httpResponse.getEntity());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
throw new Exception(e);
} finally {
getMethod.abort();
}
return result;
}
/**
处理httpResponse信息,返回String
@param httpEntity
@return String
*/
protected static String retrieveInputStream(HttpEntity httpEntity) {
int length = (int) httpEntity.getContentLength();
if (length < 0) length = 10000;
StringBuffer stringBuffer = new StringBuffer(length);
try {
InputStreamReader inputStreamReader = new InputStreamReader(httpEntity.getContent(), HTTP.UTF_8);
char buffer[] = new char[length];
int count;
while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
stringBuffer.append(buffer, 0, count);
}
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return stringBuffer.toString();
}
}
大家知道怎么回事吗
http或socket
Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信。两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务器端才能向客户端返回数据。而Socket通信则是在双方建立起连接后就可以直接进行数据的传输,在连接时可实现信息的主动推送,而不需要每次由客户端想服务器发送请求。 那么,什么是socket?Socket又称套接字,在程序内部提供了与外界通信的端口,即端口通信。通过建立socket连接,可为通信双方的数据传输传提供通道。socket的主要特点有数据丢失率低,使用简单且易于移植。
大同小异 都是你传入一个URL地址返回数据