04 |
import org.apache.http.HttpResponse;
|
05 |
import org.apache.http.HttpStatus;
|
06 |
import org.apache.http.client.HttpClient;
|
07 |
import org.apache.http.client.methods.HttpGet;
|
08 |
import org.apache.http.impl.client.DefaultHttpClient;
|
09 |
import org.apache.http.util.EntityUtils;
|
10 |
import org.json.JSONException;
|
11 |
import org.json.JSONObject;
|
12 |
import org.json.JSONTokener;
|
15 |
import android.app.Activity;
|
16 |
import android.os.Bundle;
|
17 |
import android.view.View;
|
18 |
import android.view.View.OnClickListener;
|
19 |
import android.widget.Button;
|
20 |
//import android.widget.EditText; |
21 |
import android.widget.TextView;
|
24 |
public class Http_testActivity extends Activity {
|
25 |
/** Called when the activity is first created. */ |
27 |
public void onCreate(Bundle savedInstanceState) {
|
28 |
super .onCreate(savedInstanceState);
|
29 |
setContentView(R.layout.main); |
32 |
final TextView tv = (TextView) findViewById(R.id.result);
|
33 |
//final EditText ed = (EditText) findViewById(R.id.sendurl); |
34 |
Button bt = (Button) findViewById(R.id.send); |
37 |
bt.setOnClickListener( new OnClickListener() { // 创建第一个单击事件
|
40 |
public void onClick(View v) {
|
42 |
String strResult = null ;
|
46 |
String httpUrl = "http://10.10.10.10:61002/userMessage/cJobConsultationUnread.json?data=688656&client_id=20012&view_id=268800" ;
|
48 |
HttpGet httpRequest = new HttpGet(httpUrl);
|
50 |
HttpClient httpclient = new DefaultHttpClient();
|
51 |
// 请求HttpClient,取得HttpResponse |
52 |
HttpResponse httpResponse = httpclient.execute(httpRequest); |
54 |
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
56 |
strResult = EntityUtils.toString(httpResponse |
58 |
tv.setText(strResult); |
64 |
} catch (Exception e) {
|
69 |
//返回的json串strResult={"status":0,"message":"OK","data":15} |
72 |
JSONTokener jsonParser = new JSONTokener(strResult);
|
73 |
JSONObject js = (JSONObject) jsonParser.nextValue();
|
75 |
System.out.println( "status的值是:" +js.getString( "status" ));
|
76 |
System.out.println( "message的值是:" +js.getString( "message" ));
|
77 |
System.out.println( "data的值是:" +js.getInt( "data" ));
|
79 |
} catch (JSONException ex) {
|