gpt4 book ai didi

java.lang.String 无法转换为 JSONObject

转载 作者:行者123 更新时间:2023-12-01 11:35:30 27 4
gpt4 key购买 nike

我正在尝试解析从服务器接收到的以下 JSON

{"status":1,"data":{"msg":"已添加库存","inventory_id":2001}}

使用下面

try 
{
jObj = new JSONObject(json);
final Integer status = jObj.getInt("status");

if(status == 1) {
msg="Inventory Created Successfully";
} else {
msg = jObj.getString("data");
}

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder.setTitle("Info")
.setMessage(msg)
.setCancelable(false)
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
if(status==1)
{
displayView(6);
}
}
});

AlertDialog alert = builder.create();
alert.show();

} catch (JSONException e) {
Log.e("msgtest", "unexpected JSON exception", e);
// Do something to recover ... or kill the app.
}
<小时/>

日志显示

E/msgtest﹕ unexpected JSON exception

org.json.JSONException: Value org.apache.http.conn.BasicManagedEntity@41f14cd0 of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:158)
at org.json.JSONObject.<init>(JSONObject.java:171)
at info.androidhive.wolf.NewInventoryFragment$submitAction.onPostExecute(NewInventoryFragment.java:575)
at info.androidhive.wolf.NewInventoryFragment$submitAction.onPostExecute(NewInventoryFragment.java:388)

第 #575 行jObj = new JSONObject(json);

<小时/>

异步任务

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
File file1 = new File(selectedPath1);
File file2 = new File(selectedPath2);
File file3 = new File(selectedPath3);
File file4 = new File(selectedPath4);
File file5 = new File(selectedPath5);
String urlString="http://url/index.php";

List<NameValuePair> params = new ArrayList<NameValuePair>();

try
{
FileBody bin2 = new FileBody(file2);
FileBody bin3 = new FileBody(file3);
FileBody bin4 = new FileBody(file4);
FileBody bin5 = new FileBody(file5);

params.add(new BasicNameValuePair("section","inventory"));
params.add(new BasicNameValuePair("action","new"));
params.add(new BasicNameValuePair("apron_id",stock_number_ele.getText().toString()));
params.add(new BasicNameValuePair("nickname",nick_name_ele.getText().toString()));
params.add(new BasicNameValuePair("location",location));
params.add(new BasicNameValuePair("manufacture",manufacture));
params.add(new BasicNameValuePair("core_material",core_material_ele.getText().toString()));
params.add(new BasicNameValuePair("color",color_ele.getText().toString()));
params.add(new BasicNameValuePair("Date_purchase",dop_ele.getText().toString()));
params.add(new BasicNameValuePair("UID_no",gtin_uid_ele.getText().toString()));
params.add(new BasicNameValuePair("serial",serial_ele.getText().toString()));
params.add(new BasicNameValuePair("Batch",batch_lot_ele.getText().toString()));
params.add(new BasicNameValuePair("Expiration",ed_ele.getText().toString()));
params.add(new BasicNameValuePair("garment_type",description_ele.getText().toString()));
params.add(new BasicNameValuePair("QTY",county_ele.getText().toString()));
params.add(new BasicNameValuePair("user_id",SignInActivity.user_id));*/

HttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(urlString);

FileBody bin1 = new FileBody(file1);
FileBody bin2 = new FileBody(file2);
FileBody bin3 = new FileBody(file3);
FileBody bin4 = new FileBody(file4);
FileBody bin5 = new FileBody(file5);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("section", new StringBody("inventory"));
reqEntity.addPart("action", new StringBody("new"));
reqEntity.addPart("apron_id", new StringBody(stock_number_ele.getText().toString()));
reqEntity.addPart("nickname", new StringBody(nick_name_ele.getText().toString()));
reqEntity.addPart("location", new StringBody(location+""));
reqEntity.addPart("manufacture", new StringBody(manufacture+""));
reqEntity.addPart("core_material", new StringBody(core_material_ele.getText().toString()));
reqEntity.addPart("color", new StringBody(color_ele.getText().toString()));
reqEntity.addPart("Date_purchase", new StringBody(dop_ele.getText().toString()));
reqEntity.addPart("UID_no", new StringBody(gtin_uid_ele.getText().toString()));
reqEntity.addPart("serial", new StringBody(serial_ele.getText().toString()));
reqEntity.addPart("Batch", new StringBody(batch_lot_ele.getText().toString()));
reqEntity.addPart("Expiration", new StringBody(ed_ele.getText().toString()));
reqEntity.addPart("garment_type", new StringBody(description_ele.getText().toString()));
reqEntity.addPart("QTY", new StringBody(county_ele.getText().toString()));
reqEntity.addPart("user_id", new StringBody(SignInActivity.user_id));

if(selectedPath1!="NONE")
reqEntity.addPart("image1", bin1);

if(selectedPath2!="NONE") {
Log.d("Image2","Image2");
reqEntity.addPart("image2", bin2);
}

if(selectedPath3!="NONE") {
Log.d("Image3","Image2");
reqEntity.addPart("image3", bin3);
}

if(selectedPath4!="NONE") {
Log.d("Image4","Image2");
reqEntity.addPart("image4", bin4);
}

if(selectedPath5!="NONE") {
Log.d("Image5","Image2");
reqEntity.addPart("image5", bin5);
}

post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_str = EntityUtils.toString(resEntity);

if (resEntity != null) {
Log.i("RESPONSE",response_str);
}
}
catch (Exception ex)
{
Log.e("Debug", "error: " + ex.getMessage(), ex);
}

return resEntity.toString();
}

@SuppressWarnings("deprecation")
//@Override
protected void onPostExecute(String json) {

String msg="";
JSONArray jArray=null;

if (pDialog.isShowing()) {
pDialog.dismiss();
}

try
{
jObj = new JSONObject(json);
final Integer status = jObj.getInt("status");
msg = jObj.getString("data"); // get the name from data.

if (status == 1) {
msg="Inventory Created Successfully";
}

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder.setTitle("Info")
.setMessage(msg)
.setCancelable(false)
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(status==1)
{
displayView(6);
}
}
});

AlertDialog alert = builder.create();
alert.show();

}
catch (JSONException e)
{
Log.e("msgtest", "unexpected JSON exception", e);
// Do something to recover ... or kill the app.
}
}

请提出您的看法。提前致谢。

最佳答案

您正确地使用EntityUtils.toString来读取实体的内容并作为字符串,但您在doInBackground中返回了错误的对象。您必须返回 response_str,而不是返回 resEntity.toString();

关于java.lang.String 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30032147/

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