![](/img/trans.png)
[英]get JSONException: Value of type java.lang.String cannot be converted to JSONObject when parsing a JSON response
[英]i m trying to get response from server and retrieving it using JSON. java.lang.string cannot be convert into jsonObject
我试图从服务器获取响应并使用JSON检索它,一切进行得很好,没有任何错误,但是我的IF语句未执行,程序正在执行其他执行任务,当我调试应用程序时,它说java.lang.string不能转换为jsonObject,这就是为什么它不执行它的原因。 我希望任何人都可以帮助我。 即时贴我的BackgroundTask.java和服务器响应,如果还有其他需要请告诉我。
BackgroundTask.java :
'package in.co.medimap.www.myfirstapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.EditText;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by sony on 29-04-2016.
*/
public class BackgroundTask extends AsyncTask<String,Void,String>
{
String register_url = "http://192.168.42.14/loginapp/register.php";
String login_url = "http://192.168.42.14/loginapp/login.php";
String json;
JSONArray peoples;
Context ctx;
ProgressDialog progressDialog;
Activity activity;
AlertDialog.Builder builder;
public BackgroundTask(Context ctx)
{
this.ctx=ctx;
activity = (Activity)ctx;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
builder = new AlertDialog.Builder(activity);
progressDialog = new ProgressDialog(ctx);
progressDialog.setTitle("Please Wait");
progressDialog.setMessage("Connecting to server .... ");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params)
{
String method = params[0];
if (method.equals("register")) {
try
{
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String owner_name = params[1];
String shop_name = params[2];
String phone_no = params[3];
String shop_address=params[4];
String password=params[5];
String data = URLEncoder.encode("owner_name","UTF-8")+"="+URLEncoder.encode(owner_name,"UTF-8")+"&"+
URLEncoder.encode("shop_name","UTF-8")+"="+URLEncoder.encode(shop_name,"UTF-8")+"&"+
URLEncoder.encode("phone_no","UTF-8")+"="+URLEncoder.encode(phone_no,"UTF-8")+"&"+
URLEncoder.encode("shop_address","UTF-8")+"="+URLEncoder.encode(shop_address,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line=bufferedReader.readLine())!=null)
{
stringBuilder.append(line+"\n");
}
httpURLConnection.disconnect();
Thread.sleep(5000);
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if(method.equals("login"))
{
try
{
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String phone_no,password;
phone_no=params[1];
password=params[2];
String data =URLEncoder.encode("phone_no","UTF-8")+"="+URLEncoder.encode(phone_no,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line="";
while ((line=bufferedReader.readLine())!=null)
{
stringBuilder.append(line+"\n");
}
httpURLConnection.disconnect();
Thread.sleep(5000);
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String json)
{
progressDialog.dismiss();
try
{
JSONObject jsonObject = new JSONObject(json);
peoples = jsonObject.getJSONArray("server_response");
JSONObject JO = peoples.getJSONObject(0);
String code = JO.getString("code");
String message = JO.getString("message");
//problem starts from here
if (code.equals("reg_true"))
{
showDialog("Registration Success", code, message);
}
else if (code.equals("reg_false")) {
showDialog("Registration Failed", code, message);
}
else if (code.equals("login_true")) {
Intent intent = new Intent(activity, HomeActivity.class);
intent.putExtra("message", message);
activitcode.equals("login_false")) {
showDialog("Login Error...",code, message);
}
}
//IF statement is not executing due to java.lang.string cannot be converted into jsonObject
} catch (JSONException e) {
e.printStackTrace();
}
}
public void showDialog(String title, String code, String message)
{
builder.setTitle(title);
if(code.equals("reg_true")||code.equals("reg_false"))
{
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
activity.finish();
}
});
}
else if (code.equals("login_false")) {
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText phone_no, password;
phone_no = (EditText) activity.findViewById(R.id.phone_no);
password = (EditText) activity.findViewById(R.id.password);
phone_no.setText("");
password.setText("");
dialog.dismiss();
}
});
}
AlertDialog alertDialog =builder.create();
alertDialog.show();
}
}
//server response for registration success
{"server_response":[{"code":"reg_true","0":"message=>Registration Success...Thank you....."}]}
我提出了类似的申请,这对我有用。 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.