E/JSON ;解析器:解析数据org.json.JSONException时出错:Value<;无法将java.lang.String类型的br转换为JSONObject
我正在尝试创建一个简单的android登录应用程序。我正在使用XAMPP MySQL创建数据库和服务器。android登录应用程序目前只是一个检索和验证用户登录的单一活动程序。尝试登录没有问题。但当我尝试将其应用到Android应用程序时,出现了这个问题。这是我第一次使用JSON&我检查了一个可能已经回答了我的问题的老问题。非常感谢您的帮助 Android studiologcat错误,按下登录按钮时:E/JSON ;解析器:解析数据org.json.JSONException时出错:Value<;无法将java.lang.String类型的br转换为JSONObject,java,php,android,mysql,json,Java,Php,Android,Mysql,Json,我正在尝试创建一个简单的android登录应用程序。我正在使用XAMPP MySQL创建数据库和服务器。android登录应用程序目前只是一个检索和验证用户登录的单一活动程序。尝试登录没有问题。但当我尝试将其应用到Android应用程序时,出现了这个问题。这是我第一次使用JSON&我检查了一个可能已经回答了我的问题的老问题。非常感谢您的帮助 Android studiologcat错误,按下登录按钮时: E/JSON Parser: Error parsing data org.json.JSO
E/JSON Parser: Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
E/JSON解析器:解析数据org.JSON.JSONException时出错:java.lang.String类型的值
ipconfig
//或者在mac的终端类型ifconfig中,查找en0或en1下的ip
//私有静态最终字符串登录\u URL=”http://xxx.xxx.x.x:1234/webservice/login.php";
//在模拟器上测试:
私有静态最终字符串登录\u URL=”http://10.0.2.2:1234/webservice/login.php";
//从真实服务器进行测试:
//私有静态最终字符串登录\u URL=”http://www.yourdomain.com/webservice/login.php";
//php脚本repsonse中的JSON元素ID:
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//设置输入字段
user=(EditText)findViewById(R.id.username);
pass=(EditText)findViewById(R.id.password);
//设置按钮
mSubmit=(按钮)findviewbyd(R.id.login);
mRegister=(按钮)findviewbyd(R.id.register);
//注册侦听器
mSubmit.setOnClickListener(这个);
mRegister.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.login:
字符串username1=user.getText().toString();
字符串password1=pass.getText().toString();
新建AttemptLogin().execute(username1,password1);
Log.d(“我的标签”,“这是”);
打破
案例R.id.登记册:
意图i=新意图(这是Register.class);
星触觉(i);
打破
违约:
打破
}
}
类AttemptLogin扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
布尔失败=假;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Login.this);
setMessage(“正在尝试登录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
Log.d(“myTag”,“这是我的”);
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
//检查成功标签
成功;
字符串username=args[0];
字符串密码=args[1];
Log.d(“myTag”,“这是我的消息”);
试一试{
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
Log.d(“请求!”,“启动”);
//通过发出HTTP请求获取产品详细信息
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
//检查日志中的json响应
Log.d(“登录尝试”,json.toString());
//json成功标记
success=json.getInt(TAG_success);
如果(成功==1){
Log.d(“登录成功!”,json.toString();
意图i=新意图(Login.this、ReadComments.class);
完成();
星触觉(i);
返回json.getString(TAG_消息);
}否则{
Log.d(“登录失败!”,json.getString(TAG_MESSAGE));
返回json.getString(TAG_消息);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//删除产品后关闭对话框
pDialog.disclose();
package com.example.mysqltest;
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.app.ProgressDialog;
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;
import android.widget.Toast;
public class Login extends Activity implements OnClickListener{
private EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/login.php";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//setup input fields
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
//setup buttons
mSubmit = (Button)findViewById(R.id.login);
mRegister = (Button)findViewById(R.id.register);
//register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
String username1 = user.getText().toString();
String password1 = pass.getText().toString();
new AttemptLogin().execute(username1, password1);
Log.d("myTag", "This is");
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
Log.d("myTag", "This is my");
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = args[0];
String password = args[1];
Log.d("myTag", "This is my message");
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, ReadComments.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
package com.example.mysqltest;
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() {
}
public JSONObject getJSONFromUrl(final String url) {
// Making HTTP request
try {
// Construct the client and the HTTP request.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// Execute the POST request and store the response locally.
HttpResponse httpResponse = httpClient.execute(httpPost);
// Extract data from the response.
HttpEntity httpEntity = httpResponse.getEntity();
// Open an inputStream with the data content.
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Create a BufferedReader to parse through the inputStream.
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
// Declare a string builder to help with the parsing.
StringBuilder sb = new StringBuilder();
// Declare a string to store the JSON object data in string form.
String line = null;
// Build the string until null.
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
// Close the input stream.
is.close();
// Convert the string builder data to an actual string.
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// Try to 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 the JSON Object.
return jObj;
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "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;
}
}