关于门户网站改版建设报告,小说类网站功能建设,电子商务网站建设花费,永久免费企业网站申请上一篇文章写的是服务器端利用Servlet 返回JSON字符串#xff0c;本文主要是利用android客户端访问服务器端链接#xff0c;解析JSON格式数据#xff0c;放到相应的位置上。 首先#xff0c;android程序的布局文件main.xml 1 LinearLayout xmlns:androidhttp://s…上一篇文章写的是服务器端利用Servlet 返回JSON字符串本文主要是利用android客户端访问服务器端链接解析JSON格式数据放到相应的位置上。 首先android程序的布局文件main.xml 1 LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android2 xmlns:toolshttp://schemas.android.com/tools3 android:layout_widthmatch_parent4 android:layout_heightmatch_parent5 android:paddingBottomdimen/activity_vertical_margin6 android:paddingLeftdimen/activity_horizontal_margin7 android:paddingRightdimen/activity_horizontal_margin8 android:paddingTopdimen/activity_vertical_margin9 tools:context.MainActivity
10 android:orientationvertical
11
12 TextView
13 android:idid/textView
14 android:layout_widthwrap_content
15 android:layout_heightwrap_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 Override25 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 sbnew StringBuffer();34 String urlhttp://192.168.0.251:8080/ServletTest/test;35 String bodygetContent(url);36 Log.v(222222, body);37 JSONArray arraynew JSONArray(body);38 //JSONObject tynew JSONObject(body);39 for(int i0;iarray.length();i){40 41 JSONObject objarray.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 sbnew StringBuilder();70 HttpClient clientnew DefaultHttpClient();71 HttpParams httpParamsclient.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 readernew BufferedReader(new InputStreamReader(entity.getContent()));81 String line null;82 while((linereader.readLine())! null){83 sb.append(line\n);84 }85 reader.close();86 87 }88 return sb.toString();89 90 91 }92 93 94 Override95 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设定第一启动界面开始运行 运行如下 成功啦一路上无论多么坎坷路还是要走日子还需要照过转载于:https://www.cnblogs.com/woxiangxintj/p/3963715.html