0

我在下面有这段代码:

package com.example.test.center;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Posts extends Activity {
String type,firstname,lastname,email,number,username;   
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
HttpEntity entity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Bundle gotBasket=getIntent().getExtras();
        type=gotBasket.getString("type_post");
       this.setTitle(type);

        setContentView(R.layout.addpost);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

 httpclient = new DefaultHttpClient();
 httpclient = new DefaultHttpClient();
 httppost = new HttpPost("http://192.168.1.38/LTC/Uploadposts.php?type="+type+"");
 try{
 nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("type",type));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);



 if(response.getStatusLine().getStatusCode()==200)
 {

 entity=response.getEntity();
 if(entity !=null)
 {
    InputStream instream=entity.getContent();
 JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
 //Toast.makeText(getBaseContext(),jsonResponse.toString(),Toast.LENGTH_LONG).show();
 String retUser=jsonResponse.getString("username");// username is the name of a column
 String retcont=jsonResponse.getString("Content");
 String getf=jsonResponse.getString("FirstN");
String getl=jsonResponse.getString("LastN");
String dop=jsonResponse.getString("Dateofp");


try {
    JSONArray jArray = new JSONArray(jsonResponse.toString());

    int jArrayLength = jArray.length();
    List<String> listContents = new ArrayList<String>(jArrayLength);

    for(int i =0; i<jArray.length(); i++){

        JSONObject json_data = jArray.getJSONObject(i);
        listContents.add(json_data.getString("username"));

    }

    ListView myListView = (ListView) findViewById(R.id.listView2);
    myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContents));

}catch(JSONException e){
    Log.e("log_tag","Error parsin data "+e.toString());
}

 }}

 }//End of first try

 catch(Exception e)
 {
     Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();

 } // End of first catch



    } //End of oncreate bundle



      private static String convertStreamToString(InputStream is)
       {


       BufferedReader reader = new BufferedReader(new InputStreamReader(is));
       StringBuilder sb = new StringBuilder();
       String line = null;
       try
       {

       while ((line = reader.readLine()) != null) {
           sb.append(line + "\n");
       }
       }catch(IOException e)
       {
        e.printStackTrace();
       }
       finally
       {
        try{
            is.close();
        }catch(IOException e)
        {
            e.printStackTrace();
        }
        }return sb.toString();



       }


} //End of Class

它没有正确读取 json 格式,我想获取我的 php 文件的结果(结果为 json 格式)并将其存储在 json 数组中,然后每次获取“用户名”列的值并将其加载到列表显示。有什么帮助吗?

4

2 回答 2

0

为了使上面的代码能够使用 [] 将您的 json 响应括起来。您将获得一个 JSonObject 并且您正在尝试将其解析为 JSONARRAY ...

      [{"username":"you","Dateofp":"2013-03-03","Content":"test1","IDpost:"1","LastN":"‌​    Yaghi","Type":"CARS","FirstN":"Mounzer"}] 

或使用简单的 JSON 对象解析并将对象添加到 listContents.. 删除与 JSONARRAY 相关的代码

改变

  {"IDpost":"1","username":"you","FirstN":"Mounzer","LastN":"Yaghi","Content":"tes‌​t1","Type":"CARS","Dateofp":"2013-03-03"}{"IDpost":"2","username":"boss","FirstN"‌​:"Marwan","LastN":"Geha","Content":"test2\r\n","Type":"CARS","Dateofp":"2013-03-0‌​5"}

  [{"IDpost":"1","username":"you","FirstN":"Mounzer","LastN":"Yaghi","Content":"tes‌​t1","Type":"CARS","Dateofp":"2013-03-03"},{"IDpost":"2","username":"boss","FirstN"‌​:"Marwan","LastN":"Geha","Content":"test2\r\n","Type":"CARS","Dateofp":"2013-03-0‌​5"}]

注意两个对象之间添加的“[”“]”和“,”。

于 2013-03-09T10:19:11.597 回答
0

你不能这样做:JSONArray jArray = new JSONArray(jsonResponse.toString()); jsonResponse 包含 JSONObject 而不是 JSONArray

于 2013-03-09T10:19:58.177 回答