我正在遵循一个教程,据我所知,我已经做了一切正确的交码。但是我一直在log cat中出现错误
org.json.jsonexception: value... of type org.json.JSONObject cannot be converted into jsonarray
我知道可能有人问这个问题,但他们似乎都没有遇到我遇到的问题。 我的JSONParser类
package com.nangu.r.a.t.s.JSONParser;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class JSONParser {
public JSONArray GetDriverDetails(int driver_id)
{
String url = "http://10.0.2.2/www.ratsa.com/New_folder/refatted_db/get_driver_details.php?driver_id="+driver_id;
HttpEntity httpEntity = null;
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
//Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if(httpEntity != null){
try{
String enttityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ",enttityResponse);
jsonArray = new JSONArray(enttityResponse);
}catch(JSONException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
}
和我的主要活动
package com.nangu.r.a.t.s;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.nangu.r.a.t.s.JSONParser.JSONParser;
public class MainActivity extends Activity implements OnClickListener {
private Button scanBtn;
private TextView formatTxt, resultTxt;
String scanContent;
EditText etDriverId;
int driver_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanBtn = (Button) findViewById(R.id.btnScan);
resultTxt = (TextView) findViewById(R.id.tvResult);
etDriverId = (EditText) findViewById(R.id.etDriverId);
scanBtn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnScan) {
/*
* IntentIntegrator scanIntegrator = new IntentIntegrator(this);
* scanIntegrator.initiateScan();
*/
scanContent = etDriverId.getText().toString();
driver_id = Integer.parseInt(scanContent);
//this means the did is assigned.
new GetDriverDetails().execute(new JSONParser());
}
}
public class GetDriverDetails extends AsyncTask<JSONParser, Long, JSONArray>
{
@Override
protected JSONArray doInBackground(JSONParser... params) {
//this is done in background thread
return params[0].GetDriverDetails(driver_id);
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
try{
JSONObject driver = jsonArray.getJSONObject(0);
resultTxt.setText(driver.getString("driver_first_name"));
}catch (Exception e)
{
e.printStackTrace();
}
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanningResult != null) {
scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
// contentTxt.setText("CONTENT: " + scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
如果有人能指出我正确的方向,我真的很感激。
具有实体响应的logcat:
04-13 23:40:17.768: E/Entity Response :(593): {"success":1,"driver":[{"driver_id":"1","driver_first_name":"Nangulukila ","driver_last_name":"Hacinzobolo","licence_class":"c","driver_dob":"1992-05-16","driver_adrress":"43 mulilansolo riverside","reg_date":"2011-04-05","reg_expiry":"2015-05-16"}]}
04-13 23:40:17.788: W/System.err(593): org.json.JSONException: Value {"success":1,"driver":[{"licence_class":"c","reg_expiry":"2015-05-16","driver_last_name":"Hacinzobolo","driver_dob":"1992-05-16","driver_id":"1","reg_date":"2011-04-05","driver_adrress":"43 mulilansolo riverside","driver_first_name":"Nangulukila "}]} of type org.json.JSONObject cannot be converted to JSONArray
答案 0 :(得分:0)
感谢帮助人找到问题....这是在我的PHP脚本愚蠢的错误我正在使用脚本编写一个不同的项目......