Php json-Value<;无法将java.lang.String类型的br转换为JSONObject
我正在尝试登录我的应用程序,访问我的在线数据库并检查我的输入是否已注册,但我遇到了以下错误:java.lang.String类型的值Php json-Value<;无法将java.lang.String类型的br转换为JSONObject,php,android,json,Php,Android,Json,我正在尝试登录我的应用程序,访问我的在线数据库并检查我的输入是否已注册,但我遇到了以下错误:java.lang.String类型的值
我不知道为什么我的php代码返回
。 (我在<和br之间添加了空格,以便显示) 这是我的密码
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class login extends Activity {
Button btnLogIn;
Intent intent;
JSONObject jsonobject;
Button btnSignUp;
EditText txtUsername, txtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogIn = (Button)findViewById(R.id.btnLogIn);
btnSignUp = (Button)findViewById(R.id.btnSignUp);
txtUsername = (EditText)findViewById(R.id.txtliusername);
txtPassword = (EditText)findViewById(R.id.txtlipassword);
// jsonobject = JSONfunctions.getJSONfromURL("http://sql18.hostinger.ph/phpmyadmin/index.php?db=u897407316_tret&lang=en&token=6afd355a23affd65cb4d05f814cc7921&phpMyAdmin=e2ba129883c7c52bc7e30fb543d3fa1095372c90");
}
public void gosignup(View view){
intent = new Intent(this, SignUp.class);
startActivity(intent);
}
public void gologin(View view){
String name = txtUsername.getText().toString();
String pw = txtPassword.getText().toString();
String type = "login";
if((name.trim().equals(""))||pw.trim().equals("")){
Toast.makeText(getApplicationContext(), "Please fill up all information", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Logging in...", Toast.LENGTH_SHORT).show();
new LogInAction(this).execute(name, pw);
// intent = new Intent(this, UserHomeDrawer.class);
//startActivity(intent);
}
}
}
LogInAction.java
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by Camille on 11/13/2016.
*/
public class LogInAction extends AsyncTask<String, Void, String> {
private Context context;
public LogInAction(Context context) {
this.context = context;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
String username = arg0[0];
String password = arg0[1];
String link;
String data;
BufferedReader bufferedReader;
String result;
try {
data = "?username=" + URLEncoder.encode(username, "UTF-8");
data += "&password=" + URLEncoder.encode(password, "UTF-8");
link = "http://threatmam.esy.es/loginActionAndroid.php" + data;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
String jsonStr = result.toString();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// JSONObject jsonObj = new JSONObject(jsonStr.substring(jsonStr.indexOf("{"), jsonStr.lastIndexOf("}") + 1));
String query_result = jsonObj.getString("query_result");
if (query_result.equals("SUCCESS")) {
Toast.makeText(context, "Log in successful!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,UserHomeDrawer.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else if (query_result.equals("FAILURE")) {
Toast.makeText(context, "Log in failed.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("JSON", "Error:",e);
Toast.makeText(context, ""+e+"RES: "+jsonStr, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
}
}
}
导入android.content.Context;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.Toast;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.UnsupportedEncodingException;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.net.urlcoder;
/**
*卡米尔于2016年11月13日创作。
*/
公共类登录扩展了异步任务{
私人语境;
公共登录(上下文){
this.context=上下文;
}
受保护的void onPreExecute(){
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
字符串用户名=arg0[0];
字符串密码=arg0[1];
串链;
字符串数据;
BufferedReader BufferedReader;
字符串结果;
试一试{
data=“?username=“+URLEncoder.encode(用户名,“UTF-8”);
数据+=”&password=“+urlcoder.encode(密码,“UTF-8”);
链接=”http://threatmam.esy.es/loginActionAndroid.php“+数据;
URL=新的URL(链接);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
bufferedReader=新的bufferedReader(新的InputStreamReader(con.getInputStream());
结果=bufferedReader.readLine();
返回结果;
}捕获(例外e){
返回新字符串(“异常:+e.getMessage());
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
字符串jsonStr=result.toString();
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//JSONObject jsonObj=新的JSONObject(jsonStr.substring(jsonStr.indexOf(“{”),jsonStr.lastIndexOf(“}”)+1);
字符串查询结果=jsonObj.getString(“查询结果”);
if(查询结果等于(“成功”)){
Toast.makeText(上下文,“登录成功!”,Toast.LENGTH_SHORT.show();
Intent Intent=新Intent(上下文,userhomeprawer.class)
.setFlags(意图.FLAG\u活动\u新任务);
背景。开始触觉(意图);
}else if(查询结果等于(“失败”)){
Toast.makeText(上下文“登录失败”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(上下文“无法连接到远程数据库”,Toast.LENGTH_SHORT.show();
}
}捕获(JSONException e){
e、 printStackTrace();
Log.d(“JSON”,“错误:”,e);
Toast.makeText(上下文“+e+”RES:+jsonStr,Toast.LENGTH_SHORT).show();
}
}否则{
Toast.makeText(上下文,“无法获取任何JSON数据”,Toast.LENGTH_SHORT.show();
}
}
}
这是我的PHP代码
<?php
$un = $_GET['username'];
$pw = md5(sha1($_GET['password']));
$res = 0;
include "connect.php";
$query = "SELECT * FROM tbl_mobileuser";
result = $mysqli->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if(($un == $row['Username']) && if($pw == $row['Password'])){
echo '{"query_result":"SUCCESS"}';
$res = 1;
}
}
}
else{
echo '{"query_result":"FAILURE"}';
}
if($res == 0){
echo '{"query_result":"FAILURE"}';
}
?>
我确实在寻找可能的解决办法,但我仍然无法解决这个问题。我希望有人能帮我。我点击了你的网址如下: 来自服务器的响应为:
分析错误:语法错误,第10行的/home/u897407316/public_html/loginActionAndroid.php中出现意外的“=”
看起来您的答案就在这里-请求在服务器端失败,并返回一个包含HTML的错误响应,您正在尝试将其转换为JSON。很自然,这是行不通的
确保您正在向服务器发送正确的请求,并且服务器代码工作正常
也可以考虑使用其中一个优秀的HTTP请求库(VoLLY、OKHTTP、改装等),而不是使用<代码> httpUpLink连接< /代码>来滚动自己的解决方案。
看起来,您在第11行中错过了<代码> $<代码>。
变,result = $mysqli->query($query);
对,
注意:您没有正确读取php脚本的响应
readLine()
只读取一行。如果您的php脚本输出多个(现在是这样),您的android代码将无法读取它,您将丢失一些数据。Log.d(“JSON”,jsonStr)
。。。调试请求,您将清楚地看到php脚本中的问题。这是输出分析错误:语法错误,第10行的loginActionAndroid.php中出现意外的“=”
$result = $mysqli->query($query);