2013-03-09 85 views
0

我有我用来发送信息JSON格式到Android的PHP文件,这是php文件的代码:发送到JSON数组不填充的所有列表视图项

<?php 

$host="localhost"; 
$user="root"; 
$pass=""; 
$dbname="ltc"; 

$type=$_GET['type']; 



$con= mysql_connect($host,$user,$pass); 
$BD= mysql_select_db($dbname, $con); 

$query=mysql_query("select * from posts where type='".$type."'"); 
$num=mysql_num_rows($query); 


if($num>=1) 
{ 
    while($list=mysql_fetch_assoc($query)) 
    { 

     $output=$list; 
     echo json_encode($output); 
    } 
     mysql_close(); 
    } 
    else 
    { 
    echo "error"; 
    } 
?> 

的信息安卓如下:

{"IDpost":"1","username":"you","FirstN":"Mounzer","LastN":"Yaghi","Content":"test1","Type":"CARS","Dateofp":"2013-03-03"}{"IDpost":"2","username":"boss","FirstN":"Marwan","LastN":"Geha","Content":"test2\r\n","Type":"CARS","Dateofp":"2013-03-05"} 

现在的Android我试图填充使用JSON数组一些信息列表视图,在Android的代码是:

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,content; 
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)); 
nameValuePairs.add(new BasicNameValuePair("FirstN",firstname)); 
nameValuePairs.add(new BasicNameValuePair("LastN",lastname)); 
nameValuePairs.add(new BasicNameValuePair("Content",content)); 

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"); 
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("FirstN")+" "+json_data.getString("LastN")+"     "+json_data.getString("Dateofp")+"\n\n"+json_data.getString("Content")); 

    } 

    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 

问题是,只有数据库表的第一行正在填充listview,我的意思是只有listview中的第一项正在填充,它应该是两个项目(从数据库表中取两行) 任何帮助?

+0

停止使用'mysql_ *'功能!看看这里,而不是:http://www.php.net/manual/en/mysqlinfo.api.choosing.php – Alfo 2013-03-09 17:07:30

回答

0

只是移动json_encodewhile块:

$output = array(); 
while ($list = mysql_fetch_assoc($query)) { 
    $output[] = $list; 
} 

echo json_encode($output); 

所以你在填充阵列中的所有数据,然后在循环之后立刻发送这一切。

+0

对不起,但它没有工作 – moonwalker 2013-03-09 16:41:10

+0

'json_encode'产生什么?另外请注意,您必须更改Java代码才能使用对象数组。 – dfsq 2013-03-09 16:42:38

+0

它的产生如下: [{“IDpost”:“1”,“username”:“you”,“FirstN”:“Mounzer”,“LastN”:“Yaghi”,“Content”:“test1”,“Type “:” CARS “ ”Dateofp“: ”2013年3月3日“},{ ”IDpost“: ”2“, ”用户名“: ”老板“, ”FirstN“: ”马尔“, ”LastN“:” 格哈“,”内容“:”test2 \ r \ n“,”类型“:”CARS“,”Dateofp“:”2013-03-05“}] 我应该改变什么java请帮助我 – moonwalker 2013-03-09 16:47:36

0

那么,你从上面看到的返回无效的json。做什么dfsq在php代码中说。 之后,在您的java代码中,您可以将json字符串转换为JSONArray,并使用getJSONObject方法访问数组中的每个对象。

JSONArray jsonArray = new JSONArray(convertStreamToString(instream)); 
int length = jsonArray.length(); 

for (int i=0; i < length; i++) { 
    JSONObject row = jsonArray.getJSONObject(i); 

    // Do whatever you want to do with the row object here 
} 
+0

,但你没有改变我的Java代码中的任何东西,我所需要的是要知道要在代码中做什么改变我的计划工作。谢谢 – moonwalker 2013-03-10 08:46:14

+0

@moonwalker你只需要使用行对象来填充你的'listContents'变量,最后你必须清理你的代码并删除不必要的变量,多个定义等。 这很奇怪,你无法将我的回答你的代码。 – jithujose 2013-03-10 10:59:21