Java 尝试解析JSON对象时强制关闭Android
我已经使用jersey创建了RESTWeb服务,它返回JSON响应。web服务返回的JSON响应如下-Java 尝试解析JSON对象时强制关闭Android,java,android,json,rest,Java,Android,Json,Rest,我已经使用jersey创建了RESTWeb服务,它返回JSON响应。web服务返回的JSON响应如下- { "Disease": "Bacterial_blight", "Control": "Foliar sprays of streptocycline sulphate @ 0.5 gm/land copper-oxychlode @ 3 g / l of water as and when symptoms seen." } 我制作了Android应用程序活动,用于演示,其中包含
{
"Disease": "Bacterial_blight",
"Control": "Foliar sprays of streptocycline sulphate @ 0.5 gm/land copper-oxychlode @ 3 g / l of water as and when symptoms seen."
}
我制作了Android应用程序活动,用于演示,其中包含一个单选按钮、一个编辑文本框和一个按钮,用于向REST web服务提交参数。但问题是,当我试图点击提交按钮时,我已经迫在眉睫了
这是实际的android活动类代码-
package com.doitgeek.agroadvisorysystem;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
public class DiseaseResultActivity extends AppCompatActivity {
public TextView diseaseTV;
public TextView controlMechanismTV;
public EditText etSymptom;
public RadioButton rbL;
public Button btnSubmit;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disease_result);
diseaseTV = (TextView)findViewById(R.id.diseaseTV);
controlMechanismTV = (TextView)findViewById(R.id.controlMechanismTV);
etSymptom = (EditText)findViewById(R.id.etSymptom);
rbL = (RadioButton)findViewById(R.id.rbL1);
btnSubmit = (Button)findViewById(R.id.btnSubmit);
}
public void onClickSubmit(View view) {
RequestParams params = new RequestParams();
String affectedPart = rbL.getText().toString();
String symptom = etSymptom.getText().toString();
params.put("affectedPart", affectedPart);
params.put("symptom", symptom);
invokeWS(params);
}
/* Invocation of RESTful WS */
public void invokeWS(RequestParams params) {
dialog.show();
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.0.100:8080/AgroAdvisorySystem/webapi/disease_prediction/result", params, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String response) {
dialog.hide();
try {
JSONObject obj = (JSONObject)new JSONTokener(response.toString()).nextValue();
JSONObject obj2 = obj.getJSONObject("Disease");
String disease = obj2.toString();
/*JSONObject obj = new JSONObject(response);
String disease = obj.getJSONObject("Disease").toString();*/
diseaseTV.setText(disease);
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error Occurred [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Throwable error, String content) {
dialog.hide();
if(statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if(statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occurred! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
}
直到现在,我还没有找到有效的解决方案,这就是为什么我把这个问题作为问题发布。那么,异常记录是什么 问题似乎在于:
JSONObject obj2=obj.getJSONObject(“疾病”)代码>
其中项目Disease
不再是JSONObject
试试看“疾病”
obj.getSyting(“疾病”)