ich die Daten von PHP retriving json
auf Android zu verwenden, wenn myjson Zeichenfolge retriving alle Werte von PHP hat, wenn ich benutzeJson Array ist null, während JSONObject den Wert
array= j.getJSONArray("result");
j das erste Array-Element hat aber mein Array ist null. Plz jemand sagen Sie mir die genaue Grund für diese
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://futuroitsolutions.in/php_android_api/data1.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
} finally {
try {
if (inputStream != null) inputStream.close();
} catch (Exception squish) {
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
myJSON = result;
ShowList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
public void ShowList() {
try {
String vi=myJSON;
vi=vi.replace('[',' ').replace(']',' ');
JSONObject j = new JSONObject(vi);
array= j.getJSONArray("result");
try {
for(int i=0;i<array.length();i++){
JSONObject c = array.getJSONObject(i);
id = c.getString(TAG_ID);
name = c.getString(TAG_NAME);
km = c.getString(TAG_KILOMETER);
time = c.getString(TAG_TIMING);
// get your data from jsonobject
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// String regno = c.getString(TAG_REG);
// String phone = c.getString(TAG_PHONE);
// String category = c.getString(TAG_CAT);
}
catch (JSONException e) {
e.printStackTrace();
}
TableLayout tv = (TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag = 1;
int c=array.length();
for (int i = 0; i < c ; i++) {
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b6 = new TextView(MainActivity.this);
b6.setText("Id");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);
TextView b19 = new TextView(MainActivity.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Task name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);
TextView b29 = new TextView(MainActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("Timing");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);
tv.addView(tr);
TextView b21 = new TextView(MainActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("Kilometers");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b21);
tv.addView(tr);
final View vline = new View(MainActivity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
//JSONObject json_data = jArray.getJSONObject(i);
// Log.i("log_tag", "id: " + json_data.getInt("Id") + ", Username: " + json_data.getString("Task name") + ", No: " + json_data.getString("Kilometers"));
TextView b = new TextView(MainActivity.this);
String stime =id;
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(MainActivity.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1 = name;
b1.setText(stime1);
b1.setTextColor(Color.BLACK);
tr.addView(b1);
TextView b2 = new TextView(MainActivity.this);
b2.setPadding(10, 0, 0, 0);
String stime2 = time;
b2.setText(stime2);
b2.setTextColor(Color.BLACK);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
TextView b3 = new TextView(MainActivity.this);
b2.setPadding(10, 0, 0, 0);
String stime3 = km;
b2.setText(stime3);
b2.setTextColor(Color.BLACK);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
final View vline1 = new View(MainActivity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
}
}
wenn ich debuggen, bekomme ich
vi=
{"id":"1","Taskname":"edapally to kakkanad","SourceLa":"10.0236761","SourceLo":"76.311623","Path1La":"10.0327153","Path1Lo":"76.331889","Path2La":"10.0231412","Path2Lo":"76.3409954","DestinationLa":"10.0158605","DestinationLo":"76.3418666","Timing":"10 a.m","Kilometer":"8"}
j={"id":"1","Taskname":"edapally to kakkanad","SourceLa":"10.0236761","SourceLo":"76.311623","Path1La":"10.0327153","Path1Lo":"76.331889","Path2La":"10.0231412","Path2Lo":"76.3409954","DestinationLa":"10.0158605","DestinationLo":"76.3418666","Timing":"10 a.m","Kilometer":"8"}
Array = null meine Json
[{“ id ":" 1 "," Aufgabenname ":" edapally to kakkanad "," SourceLa ":" 10.0236761 "," SourceLo ":" 76.311623 "," Path1La ":" 10.032715 3 "," Path1Lo ":" 76.331889 "," Path2La ":" 10.0231412 "," Path2Lo ":" 76.3409954 "," DestinationLa ":" 10.0158605 "," DestinationLo ":" 76.3418666 "," Timing ":" 10 Uhr morgens “, "Kilometer": "8"}]
Data1.php
<?php
include('sql.php');
mysql_query('SET CHARACTER SET utf8') ;
$result = mysql_query('SELECT * FROM Location') or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$rows[] = $row;
}
$final=array("result"=>$rows);
echo json_encode($rows);
?>
Sie erhalten JSONObject nicht JSONArray in vi wie Sie gesagt haben. –
Wenn Sie ein 'result' wollen, sollten Sie $ final, nicht $ rows in Ihrem PHP kodieren. – YvesLeBorg
ya ich kodierte nur final, aber es gab auch Array null, so dass ich nur $ Zeilen kodierte und auch wenn ich kodiere, bekomme ich JSON wie {"results" ............ [...... ......]} –