我的注册AsyncTask工作正常,我的数据保存在PHP MYSQL工作正常,但我遇到了异常。
public class SignupJson extends AsyncTask<String,String,String> {
//SignupJSONParser sjson = new SignupJSONParser();
JSONObject json = new JSONObject();
//private static final String TAG_SUCCESS = "success";
HttpParams mhttpparams = new BasicHttpParams();
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(SignupActivity.this);
pDialog.setMessage("Creating Account..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
String getName = _Name.getText().toString();
String getEmail = _Email.getText().toString();
String getPassword = _Password.getText().toString();
List<NameValuePair> pair = new ArrayList<NameValuePair>();
pair.add(new BasicNameValuePair("name",getName));
pair.add(new BasicNameValuePair("id",getEmail));
pair.add(new BasicNameValuePair("pass", getPassword));
// int success=0;
json = JSONParser.makeHttpRequest( URL,"POST", pair);
Log.d("Create Response", json.toString());
try {
String success = json.getString(TAG_SUCCESS);
//success = json.getInt(TAG_SUCCESS);
if (json.getString(TAG_SUCCESS)!=null) {
if (Integer.parseInt(success)==1)
{
Log.d("User Created!", json.toString());
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(login);
// closing this screen
finish();
//return json.getString(TAG_MESSAGE);
}
else {
Log.d("Registration Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
}
} catch (JSONException e) {
e.printStackTrace();
// success=0;
}
return null;
// return ""+success;
}
@Override
protected void onPostExecute(String s) {
pDialog.dismiss();
if (json != null){
Toast.makeText(getApplicationContext(),"Registration Done",Toast.LENGTH_SHORT).show();
//Toast.makeText(MainActivity.this, json.toString(), Toast.LENGTH_LONG).show();
}
}
}
}
logcat的:
com.example.ahmadkhan.routeapp W / System.err:org.json.JSONException: 没有成功的价值 /com.example.ahmadkhan.routeapp W / System.err:at org.json.JSONObject.get(JSONObject.java:354) com.example.ahmadkhan.routeapp W / System.err:at org.json.JSONObject.getString(JSONObject.java:514) /com.example.ahmadkhan.routeapp W / System.err:at com.example.ahmadkhan.routeapp.SignupActivity $ SignupJson.doInBackground(SignupActivity.java:169) com.example.ahmadkhan.routeapp W / System.err:at com.example.ahmadkhan.routeapp.SignupActivity $ SignupJson.doInBackground(SignupActivity.java:129) com.example.ahmadkhan.routeapp W / System.err:在android.os.AsyncTask $ 2.call(AsyncTask.java:287) com.example.ahmadkhan.routeapp W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:234) com.example.ahmadkhan.routeapp W / System.err:在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) com.example.ahmadkhan.routeapp W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:108com.example.ahmadkhan.routeapp W / System.err:at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:57com.example.ahmadkhan.routeapp W / System.err:at java.lang.Thread.run(Thread.java:841)
行错误
String success = json.getString(TAG_SUCCESS);
//success = json.getInt(TAG_SUCCESS);
if (json.getString(TAG_SUCCESS)!=null) {
}
任何人都可以建议我如何解决此错误的解决方案,并告诉我如何成功处理服务器响应以及与PHP Wamp Server连接失败。
答案 0 :(得分:0)
JSONObject.get*()
方法不返回null
,它们会抛出JSONException
。
E.g。 getString()
:
抛出: JSONException - 如果密钥没有字符串值。
使用JSONObject.opt*()
方法:
String success = json.optString(TAG_SUCCESS);
if (success != null) {
// ...
}
答案 1 :(得分:0)
org.json.JSONException: No value for success
//means no key-value found for this key - "success"
使用has()方法处理此异常
if(<jsonobject>.has("success"))
{
//your code
}
答案 2 :(得分:0)
尝试这个我希望它会帮助你..!
JSONObject json = JSONParser.makeHttpRequest(URL,&#34; POST&#34;,pair);
if(json!=null)
{
Log.d("Create Response", json.toString());
try {
String success = json.getString(TAG_SUCCESS);
//success = json.getInt(TAG_SUCCESS);
if (json.getString(TAG_SUCCESS)!=null) {
if (Integer.parseInt(success)==1)
{
Log.d("User Created!", json.toString());
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(login);
// closing this screen
finish();
//return json.getString(TAG_MESSAGE);
}
else {
Log.d("Registration Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
}
} catch (JSONException e) {
e.printStackTrace();
// success=0;
}
}else{
Log.d("getting null response from server!");
}