app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上

2023-05-06,,

上一篇文章写的是服务器端利用Servlet 返回JSON字符串,本文主要是利用android客户端访问服务器端链接,解析JSON格式数据,放到相应的位置上。

首先,android程序的布局文件main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </LinearLayout>

MainActivity.java

 package com.practice;

 import java.io.BufferedReader;
import java.io.InputStreamReader; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject; import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("111111111", "1111111111111111"); try{
StringBuffer sb=new StringBuffer();
String url="http://192.168.0.251:8080/ServletTest/test";
String body=getContent(url);
Log.v("222222", body);
JSONArray array=new JSONArray(body);
//JSONObject ty=new JSONObject(body);
for(int i=0;i<array.length();i++){ JSONObject obj=array.getJSONObject(i);
sb.append("id:").append(obj.getInt("id")).append("\t");
sb.append("name:").append(obj.getString("name")).append("\t");
sb.append("gender:").append(obj.getString("gender")).append("\t\n");
sb.append("email:").append(obj.getString("email")).append("\t");
sb.append("----------------------\n"); }
Log.v("333333333333", sb.toString());
TextView textView =(TextView) findViewById(R.id.textView);
textView.setText(sb.toString()); }
catch(Exception e)
{ Log.v("11111111111", e.toString());
Log.v("11111111111", "msg");
} } private String getContent(String url) throws Exception{ StringBuilder sb=new StringBuilder();
HttpClient client=new DefaultHttpClient();
HttpParams httpParams=client.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 5000); HttpResponse response =client.execute(new HttpGet(url));
HttpEntity entity =response.getEntity(); if(entity!=null){
BufferedReader reader=new BufferedReader(new InputStreamReader(entity.getContent()));
String line =null;
while((line=reader.readLine())!= null){
sb.append(line+"\n");
}
reader.close(); }
return sb.toString(); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

在AndroidManifest.xml,设定第一启动界面,开始运行

运行如下:

成功啦!一路上,无论多么坎坷,路还是要走,日子还需要照过!

app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上的相关教程结束。

《app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上.doc》

下载本文的Word格式文档,以方便收藏与打印。