2013-03-09 73 views
0

我有下面这段代码:JSON错误,同时填充一个ListView

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数组中,然后每次获取“用户名”列中的值并将其加载到列表视图中。任何帮助?

+0

什么是错误发布您的日志 – DjHacktorReborn 2013-03-09 09:40:49

+1

我不明白为什么你添加'php'标签,因为你没有显示任何脚本相关。 – 2013-03-09 09:42:53

+0

也发布您的JSON字符串 – Sedz 2013-03-09 09:43:44

回答

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"}] 

的通知 “(” “)中有两个对象之间的” 添加 “” ..

+0

非常感谢,它真的有效,但我只得到表中的一行。该表包含两行 – moonwalker 2013-03-09 10:30:43

+0

你的意思是服务器端问题,从你的服务器你发送一行或从服务器你发送两个,并在Android中显示一个? – baboo 2013-03-09 10:33:37

+0

我发送2行,但在Android它只是采取一行。我只是在我的代码中进行了更改: JSONArray jArray = new JSONArray(“[”+ jsonResponse.toString()+“]”) ; – moonwalker 2013-03-09 10:35:00

0

你不能做到这一点:JSONArray jArray = new JSONArray(jsonResponse.toString()); jsonResponse包含的JSONObject不JSONArray