gpt4 book ai didi

java - json解析错误

转载 作者:行者123 更新时间:2023-12-01 13:02:07 27 4
gpt4 key购买 nike

当我运行我的应用程序时,出现以下错误,并且 ADV 中显示空白页面。这是我在解析输入时遇到的问题:

Error parsing data org.json.JSONException: Value You of type java.lang.String cannot be converted to JSONObject.

如果我能得到一些帮助,我将不胜感激。

类 ListQuest.java

           public class ListQuest extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> questsList =
new ArrayList<HashMap<String, String>>();

// url to get all products list
private static String url_all_quests =
"http://10.0.2.2/android_connect/listquest.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_QUEST = "Question";
private static final String TAG_REP = "Reponse";
private static final String TAG_IDE = "idenq";
private static final String TAG_IDQ = "idquest";
private static final String TAG_LIBQ = "libquest";
private static final String TAG_LIBR = "librep";


// products JSONArray

String idenq;
String idquest;
String libquest,librep;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_quest);
Intent i = getIntent();
idenq = i.getStringExtra(TAG_IDE);
idquest = i.getStringExtra(TAG_IDQ);


final CheckBox checkBox = (CheckBox) findViewById(R.id.chkIos);

// Hashmap for ListView
questsList = new ArrayList<HashMap<String, String>>();

// Loading products in Background Thread
new LoadAllProducts().execute();

// Get listview
ListView lv = getListView();

// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String idquest = ((TextView) view.findViewById(R.id.idquest)).getText()
.toString();

// Starting new intent
Intent in = new Intent(getApplicationContext(),
test.class);
// sending pid to next activity
in.putExtra(TAG_IDQ, idquest);

// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});

}

// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}

}

/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListQuest.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("idenq", idenq));
params.add(new BasicNameValuePair("idquest", idquest));

params.add(new BasicNameValuePair("libquest", libquest));

params.add(new BasicNameValuePair("librep", librep));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_quests, "GET", params);

// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

try {




// products found
// Getting Array of Products
JSONArray Question = json.getJSONArray(TAG_QUEST);

// looping through All Products
for (int i = 0; i < Question.length(); i++) {


// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// Storing each json item in variable
JSONObject c=Question.getJSONObject(i);
String libquest = c.getString(TAG_LIBQ);
JSONArray Reponse =c.getJSONArray(TAG_REP);


map.put(TAG_LIBQ, libquest);

// adding HashList to ArrayList

// looping through All Products

for (int k =0 ; k < Reponse.length(); k++) {
HashMap<String, String> map1 = new
HashMap<String, String>();
JSONObject r=Reponse.getJSONObject(k);
String librep = r.getString(TAG_LIBR);
map.put(TAG_LIBR, librep);

}
questsList.add(map);
}


} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */


ListAdapter adapter = new SimpleAdapter(
ListQuest.this, questsList,
R.layout.list_quest, new String[] {
TAG_LIBQ,TAG_LIBR},
new int[] { R.id.libquest ,
R.id.librep});
// updating listview
setListAdapter(adapter);



}
});

}

}

}

JsonParser 类

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {

// Making HTTP request
try {

// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");

}
is.close();
json = sb.toString().substring(0, sb.toString().length()-1);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}
}

结果是:

        {"Question":[{"idquest":"17","libquest":"avez vous visité 
tunis?","Reponse":[{"librep":"oui"},{"librep":"non"}]},
{"idquest":"50","libquest":"quel est votre spécialité","Reponse":
[{"librep":"MP"},{"librep":"PC"}]},{"idquest":"51","libquest":"quel est
votre équipe préféré","Reponse":[]}]}

最佳答案

很可能你正在解析的字符串不是你想象的那样,而且实际上不是 json。我猜想这是某种以“你”一词开头的消息。

关于java - json解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23447623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com