Erro ao analisar dados: JSONException: Valor <br do tipo java.lang.String não pode ser convertido em JSONObject

Estou fazendo um formulário de registro. Eu tenho duas classs: ClientServer.java e JSONParser.java

ClientServer.java:

 package com.example.mapsdemo; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Clientserver extends Activity { EditText username,email,password; Button signup,intent; String mpostusername,mpostpassword,mpostemail; private static final String TAG_SUCCESS = "success"; JSONParser jsonParser = new JSONParser(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clientserver); username = (EditText) findViewById(R.id.username); email = (EditText) findViewById(R.id.email); password = (EditText) findViewById(R.id.password); signup = (Button)findViewById(R.id.signup); intent = (Button)findViewById(R.id.go); //Setup the Button's OnClickListener signup.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Get the data new CreateNewAccount().execute(); Log.d("my", "in onclick...create new account executed"); } }); intent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(Clientserver.this,MainActivity.class); startActivity(i); } }); } class CreateNewAccount extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ /** * Creating product * */ protected String doInBackground(String... args) { Log.d("my", "in doInBackground()"); mpostusername = username.getText().toString(); mpostemail = email.getText().toString(); mpostpassword = password.getText().toString(); Log.d("my", "getting inputs is executed"); List params = new ArrayList(); params.add(new BasicNameValuePair("username",mpostusername)); params.add(new BasicNameValuePair("email",mpostemail)); params.add(new BasicNameValuePair("password",mpostpassword)); Log.d("my", "parameter adding executed"); JSONObject json = jsonParser.makeHttpRequest("http:///android_connect/register.php","POST", params); if(json!=null){ // do something Log.d("my", "http request made successfully"); try { int success = json.getInt(TAG_SUCCESS); Log.d("my", "JSON tag_success is working"); if (success == 1) { Log.d("my", "in success=1"); // successfully created product Intent i = new Intent(getApplicationContext(), MainActivity.class); startActivity(i); // closing this screen finish(); } else { Log.d("my", "In else"); } } catch (JSONException e) { e.printStackTrace(); } } return null; } } } 

JSONParser.java :

 package com.example.mapsdemo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } // function get json from url // by making HTTP POST or GET mehtod public JSONObject makeHttpRequest(String url, String method, List params) { // Making HTTP request try { // check for request method if(method.equals("POST")){ // request method is POST // defaultHttpClient Log.d("my", "method equals POST is working"); DefaultHttpClient httpClient = new DefaultHttpClient(); Log.d("my", "HTTp client is working"); HttpPost httpPost = new HttpPost(url); Log.d("my", "HTTp post is working"); httpPost.setEntity(new UrlEncodedFormEntity(params)); Log.d("my", "url encoded"); HttpResponse httpResponse = httpClient.execute(httpPost); Log.d("my", "HTTp response is working"); HttpEntity httpEntity = httpResponse.getEntity(); Log.d("my", "HTTp entity is working"); is = httpEntity.getContent(); Log.d("my", "getcontent is working"); }else if(method.equals("GET")){ // request method is GET DefaultHttpClient httpClient = new DefaultHttpClient(); String paramString = URLEncodedUtils.format(params, "utf-8"); url += "?" + paramString; HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } } 

db_connect.php

 <?php $connection = mysql_connect('http://xxx.xxx.xxx.xx/phpmyadmin', 'xxxx', 'xxxxx'); if (!$connection){ die("Database Connection Failed" . mysql_error()); } $select_db = mysql_select_db('accountsb'); if (!$select_db){ die("Database Selection Failed" . mysql_error()); } 

register.php

  

ERRO:

0-06 12: 16: 45.340: E / JSON Parser (8630): Erro ao analisar dados org.json.JSONException: Valor

Aqui está o meu log filtrado logcat:

 10-06 12:31:09.800: D/my(11813): in onclick...create new account executed 10-06 12:31:09.800: D/my(11813): in doInBackground() 10-06 12:31:09.800: D/my(11813): getting inputs is executed 10-06 12:31:09.800: D/my(11813): parameter adding executed 10-06 12:31:09.800: D/my(11813): method equals POST is working 10-06 12:31:09.800: D/my(11813): HTTp client is working 10-06 12:31:09.800: D/my(11813): HTTp post is working 10-06 12:31:09.800: D/my(11813): url encoded 10-06 12:31:12.090: D/my(11813): HTTp response is working 10-06 12:31:12.090: D/my(11813): HTTp entity is working 10-06 12:31:12.090: D/my(11813): getcontent is working 

NOTA: 1. Eu vi muitas perguntas semelhantes em SO e tentei muitas soluções, mas não funcionou depois de tentar cerca de 2 horas. Então, não é a marca como ajuda dulicate.plz.

  1. Meu código php sofre de injeção SQL, eu sei. Mas, é pra praticar e eu os edito com mysqli e DOP no futuro.

Tente colocar o isset em seu PHP para garantir que eles obtenham todos os valores (funciona como o campo obrigatório). Também acho que o problema está em sua consulta

 $result = mysql_query("INSERT INTO accounts(email, password, username) values ('$email','$password','$username')"); 

Tente isso. Além disso, se o seu URL parece ser o problema no prompt de comando, use o ipconfig e obtenha o endereço IPv4 mais alto e faça isso como sua porta.

// Json Handler

class pública ServiceHandler {

 static String response = null; public final static int GET = 1; public final static int POST = 2; public ServiceHandler() { } /** * Making service call * @url - url to make request * @method - http request method * */ public String makeServiceCall(String url, int method) { return this.makeServiceCall(url, method, null); } /** * Making service call * @url - url to make request * @method - http request method * @params - http request params * */ public String makeServiceCall(String url, int method, List params) { try { // http client DefaultHttpClient httpClient = new DefaultHttpClient(); HttpEntity httpEntity = null; HttpResponse httpResponse = null; // Checking http request method type if (method == POST) { HttpPost httpPost = new HttpPost(url); // adding post params if (params != null) { httpPost.setEntity(new UrlEncodedFormEntity(params)); } httpResponse = httpClient.execute(httpPost); } else if (method == GET) { // appending params to url if (params != null) { String paramString = URLEncodedUtils .format(params, "utf-8"); url += "?" + paramString; } HttpGet httpGet = new HttpGet(url); httpResponse = httpClient.execute(httpGet); } System.out.println("Status in url =====" + httpResponse.getStatusLine().getStatusCode()); httpEntity = httpResponse.getEntity(); response = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } 

}