2016-11-13 149 views
-2

我想登錄我的應用程序,訪問我的在線數據庫,並檢查我的輸入是否已註冊,但我得到此錯誤:Value <類型爲java.lang.String的br不能轉換爲JSONObject。json - 值類型java.lang.String不能轉換爲JSONObject

我不知道爲什麼我的php代碼返回< br />。 (我增加了<和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(); 
     } 
    } 
} 

這是我的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"}'; 
}      
?> 

我絕對擡頭可能的解決辦法,但我仍然無法解決這個問題。我希望有一個人可以幫助我。

+0

'Log.d(「JSON」,jsonStr)'...調試請求,您將清楚地看到問題 –

+0

運行php腳本。這是輸出。 _
解析錯誤:語法錯誤,意想不到的 '=' 在loginActionAndroid.php上線
_ –

回答

0

我打你的網址如下:

http://threatmam.esy.es/loginActionAndroid.php?username=foo&password=bar

來自服務器的響應是:

<br /> <b>Parse error</b>: syntax error, unexpected '=' in <b>/home/u897407316/public_html/loginActionAndroid.php</b> on line <b>10</b><br />

看起來你有你的答案就在那裏 - 請求失敗的服務器端並返回一個錯誤響應,HTML中將嘗試轉換爲JSON。這很自然地不起作用。

確保您向服務器發送了正確的請求,並且服務器代碼工作正常。另外,您可能會考慮使用其中一個優秀的HTTP請求庫(Volley,OkHttp,Retrofit等),而不是使用HttpURLConnection來滾動您自己的解決方案。

+0

我不能給予好評,但感謝! – camille

0

看起來像是在線#11上錯過了$

變化,

result = $mysqli->query($query); 

到,

$result = $mysqli->query($query); 

注意:您沒有正確閱讀你的PHP腳本的響應。 readLine()只能讀取一行。如果您的PHP腳本輸出多個(因爲它是現在),您的android代碼將無法讀取它,你會錯過一些數據。

+0

我不能upvote,但謝謝! – camille

+0

@camille請接受它是否有幫助。 :) –

相關問題