这是我的Android代码:
public void SendDataToServer(final String name, final String email, final String password){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String QuickName = name ;
String QuickEmail = email ;
String QuickPassword = password;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nome", QuickName));
nameValuePairs.add(new BasicNameValuePair("email", QuickEmail));
nameValuePairs.add(new BasicNameValuePair("password", QuickPassword));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Configs.signup);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Submit Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d(result, "Value");
try {
JSONObject jo = new JSONObject(result);
String status = jo.optString("status");
if (status.equals("0")) {
Toast.makeText(Signup.this, "Username already exists", Toast.LENGTH_LONG).show();
} else if (status.equals("1")) {
Intent intent = new Intent(Signup.this, Login.class);
startActivity(intent);
Toast.makeText(Signup.this, "Registered successfully", Toast.LENGTH_LONG).show();
Toast.makeText(Signup.this, "Verify your email adress in email received", Toast.LENGTH_SHORT).show();
finish();
} else if (status.equals("2")) {
Toast.makeText(Signup.this, "Failed to Signup", Toast.LENGTH_LONG).show();
}
//}
}catch (JSONException e) {
e.printStackTrace();
}
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, email, password);
}
这是错误:
07-21 12:55:35.297 24973-24973 / com.futegolo.igomessenger W / System.err:
org.json.JSONException:类型为java.lang.String的值数据不能为
转换为JSONObject
这是我的json回应
{"status":0}
最佳答案
使用以下代码:
public void SendDataToServer(final String name, final String email, final String password){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String QuickName = name ;
String QuickEmail = email ;
String QuickPassword = password;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nome", QuickName));
nameValuePairs.add(new BasicNameValuePair("email", QuickEmail));
nameValuePairs.add(new BasicNameValuePair("password", QuickPassword));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Configs.signup);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
StringBuffer result= new StringBuffer();
BufferedReader in = new BufferedReader(
new InputStreamReader(entity.getContent()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
result.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d(result, "Value");
try {
JSONObject jo = new JSONObject(result);
String status = jo.optString("status");
if (status.equals("0")) {
Toast.makeText(Signup.this, "Username already exists", Toast.LENGTH_LONG).show();
} else if (status.equals("1")) {
Intent intent = new Intent(Signup.this, Login.class);
startActivity(intent);
Toast.makeText(Signup.this, "Registered successfully", Toast.LENGTH_LONG).show();
Toast.makeText(Signup.this, "Verify your email adress in email received", Toast.LENGTH_SHORT).show();
finish();
} else if (status.equals("2")) {
Toast.makeText(Signup.this, "Failed to Signup", Toast.LENGTH_LONG).show();
}
//}
}catch (JSONException e) {
e.printStackTrace();
}
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, email, password);
}
关于java - org.json.JSONException:值类型为java.lang.String的数据不能转换为JSONObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45237105/