[问题] JSON撷取资料问题

楼主: bruce0428 (bruce)   2017-11-10 19:04:28
JSON格式
{"A":"1","B":"2","C":"3","D":"4","E"{"F":"5","G":"6"},"H":"7"}
小弟利用POST方法撷取JSON数据,只打算撷取B值为2
程式码
第一部分POST包
public static String SendPostMethod(String path,String encode){
String result="";
HttpClient httpClient=new DefaultHttpClient();
try{
HttpPost post=new HttpPost(path);
HttpResponse response=httpClient.execute(post);
if(response.getStatusLine().getStatusCode()==200){
result= EntityUtils.toString(response.getEntity(),encode);
}
}catch(Exception e){
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();
}
return result;
}
}
第二部分 JSON包
public static String parseList3(String JsonString){
String list=null;
try{
JSONObject jsonObject = new JSONObject(JsonString);
String result = jsonObject.getString("B");
}catch(Exception e){
e.printStackTrace();
}
return list;
}
第三部分MainActivity
private final String City_path="XXX";
private ProgressDialog dialog;
private TextView Tv1;
private String B="5";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog= new ProgressDialog(this);
dialog.setTitle("提示");
dialog.setMessage("Loading.....");
new MyTask().execute(City_path);
Tv1=(TextView)findViewById(R.id.textView2);
}
class MyTask extends AsyncTask<String,Void,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
@Override
protected String doInBackground(String... params) {
String list=null;
String jsonSting=HttpUtils.SendPostMethod(params[0],"utf-8");
list= JsonTools.parseList3(jsonSting);
return list;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
Tv1.setText(result);
}
}
小弟想利用异步任务将JSON数据从网络上抓下来撷取B值再透过setText方法
将B值显示在TextView当中,然而模拟器log没有报错,应该是有抓到JSON数据
但是就是无法转到TextView当中,code不知道哪里错了,可以请各位高手帮忙看看吗
作者: ininmm (子虚乌有)   2017-11-10 20:47:00
httpclient 还能用吗?
楼主: bruce0428 (bruce)   2017-11-10 20:58:00
有加httpclient包
作者: lnmlee   2017-11-10 21:16:00
第二部分 parseList3 你回传的是list (null) 而不是回传取完B值的 reslut
作者: j19930307 (钙世英雄)   2017-11-10 22:11:00
result要在大括号外宣告// String list=null; => String result=null;
作者: vux (点点)   2017-11-11 00:19:00
Tv1 早点findview

Links booklink

Contact Us: admin [ a t ] ucptt.com