上一篇文章写的是服务器端利用Servlet 返回JSON字符串,本文主要是利用android客户端访问服务器端链接,解析JSON格式数据,放到相应的位置上。
首先,android程序的布局文件main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" 10 android:orientation="vertical"> 11 12 <TextView 13 android:id="@+id/textView" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 /> 17 18 </LinearLayout>
MainActivity.java
1 package com.practice; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 6 import org.apache.http.HttpEntity; 7 import org.apache.http.HttpResponse; 8 import org.apache.http.client.HttpClient; 9 import org.apache.http.client.methods.HttpGet; 10 import org.apache.http.impl.client.DefaultHttpClient; 11 import org.apache.http.params.HttpConnectionParams; 12 import org.apache.http.params.HttpParams; 13 import org.json.JSONArray; 14 import org.json.JSONObject; 15 16 import android.os.Bundle; 17 import android.app.Activity; 18 import android.util.Log; 19 import android.view.Menu; 20 import android.widget.TextView; 21 22 public class MainActivity extends Activity { 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.main); 28 Log.v("111111111", "1111111111111111"); 29 30 31 32 try{ 33 StringBuffer sb=new StringBuffer(); 34 String url="http://192.168.0.251:8080/ServletTest/test"; 35 String body=getContent(url); 36 Log.v("222222", body); 37 JSONArray array=new JSONArray(body); 38 //JSONObject ty=new JSONObject(body); 39 for(int i=0;i<array.length();i++){ 40 41 JSONObject obj=array.getJSONObject(i); 42 sb.append("id:").append(obj.getInt("id")).append("\t"); 43 sb.append("name:").append(obj.getString("name")).append("\t"); 44 sb.append("gender:").append(obj.getString("gender")).append("\t\n"); 45 sb.append("email:").append(obj.getString("email")).append("\t"); 46 sb.append("----------------------\n"); 47 48 } 49 Log.v("333333333333", sb.toString()); 50 TextView textView =(TextView) findViewById(R.id.textView); 51 textView.setText(sb.toString()); 52 53 54 55 } 56 catch(Exception e) 57 { 58 59 Log.v("11111111111", e.toString()); 60 Log.v("11111111111", "msg"); 61 } 62 63 } 64 65 66 private String getContent(String url) throws Exception{ 67 68 69 StringBuilder sb=new StringBuilder(); 70 HttpClient client=new DefaultHttpClient(); 71 HttpParams httpParams=client.getParams(); 72 73 HttpConnectionParams.setConnectionTimeout(httpParams, 3000); 74 HttpConnectionParams.setSoTimeout(httpParams, 5000); 75 76 HttpResponse response =client.execute(new HttpGet(url)); 77 HttpEntity entity =response.getEntity(); 78 79 if(entity!=null){ 80 BufferedReader reader=new BufferedReader(new InputStreamReader(entity.getContent())); 81 String line =null; 82 while((line=reader.readLine())!= null){ 83 sb.append(line+"\n"); 84 } 85 reader.close(); 86 87 } 88 return sb.toString(); 89 90 91 } 92 93 94 @Override 95 public boolean onCreateOptionsMenu(Menu menu) { 96 // Inflate the menu; this adds items to the action bar if it is present. 97 getMenuInflater().inflate(R.menu.main, menu); 98 return true; 99 } 100 101 }
在AndroidManifest.xml,设定第一启动界面,开始运行
运行如下:
成功啦!一路上,无论多么坎坷,路还是要走,日子还需要照过!