分析数据时出错:JSONException:Value<;无法将java.lang.String类型的br转换为JSONObject
我正在做登记表。我有两个类:分析数据时出错:JSONException:Value<;无法将java.lang.String类型的br转换为JSONObject,java,php,android,json,Java,Php,Android,Json,我正在做登记表。我有两个类:ClientServer.java和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.Bas
ClientServer.java
和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<String, String, String> {
/**
* 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<NameValuePair> params = new ArrayList<NameValuePair>();
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://<ip_address>/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;
}
}
}
注意:
1.我在SO上见过许多类似的问题,也尝试过许多解决方案,但在尝试了大约2个小时后都不起作用,所以,请不要将其标记为dulicate.plz帮助
尝试将isset放在PHP中,以确保它们获得所有的值(类似于必填字段),我认为问题出在您的查询中
$result = mysql_query("INSERT INTO accounts(email, password, username)
values ('$email','$password','$username')");
试试这个。另外,如果您的URL在命令提示符中出现问题,请使用ipconfig并获取最顶端的ipv4地址,并将其作为您的端口。/Json Handler
公共类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<NameValuePair> 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;
}
静态字符串响应=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
公共服务处理程序(){
}
/**
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
* */
公共字符串makeServiceCall(字符串url,int方法){
返回此.makeServiceCall(url,方法,null);
}
/**
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
*@params-http请求参数
* */
公共字符串makeServiceCall(字符串url,int方法,
列表参数){
试一试{
//http客户端
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
//检查http请求方法类型
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
//添加post参数
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
//将参数附加到url
如果(参数!=null){
String paramString=URLEncodedUtils
.格式(参数“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
System.out.println(“url中的状态===”+httpResponse.getStatusLine().getStatusCode());
httpEntity=httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回响应;
}
}发布您的
JSON
响应@ṁᾶƔƏňツ 好的,我正在编辑@ṁᾶƔƏňツ 我已经编辑过了。我想你没有得到json响应,而是得到了一些html标记。检查您是否从服务器获得了正确的响应,然后发布JSON
响应。关键是,就我们所知,您的代码可能完全正确。服务器没有返回JSON。这可能是服务器的问题,或者你可能点击了错误的URL,但我们无法从这里判断。嗨…我已经更新了我的问题…你现在能帮我吗?
<?php
require('db_connect.php');
$response = array();
$username= $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO `account` (username,email,password) VALUES ('$username', '$email', '$password')";
$result = mysql_query($query);
if($result){
$response["success"] = 1;
$response["message"] = "account successfully created.";
// echoing JSON response
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
?>
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
$result = mysql_query("INSERT INTO accounts(email, password, username)
values ('$email','$password','$username')");
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<NameValuePair> 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;
}