2013-10-24 70 views
0

我是新來的android開發,我想做一個登錄頁面,將密碼和用戶名作爲json數組發送到php腳本,並且php腳本返回一個包含相應meassage的json數組響應。從android應用發送和接收json到php腳本?

我已經做了的Android代碼:

  jobj.put("uname", userName); 
      jobj.put("password", passWord); 
      JSONObject re = JSONParser.doPost(url, jobj); 
      Log.v("Received","Response received . . ."+re); 
      // Check your log cat for JSON reponse 
      Log.v("Response: ", re.toString()); 
      int success = re.getInt("success"); 
      if (success == 1) { 
       return 1; 
      } 
      else{ 
       return 0; 
      } 
     } 
     catch(Exception e){ e.getMessage(); } 
    } 

的JsonParser doPost方法的代碼如下:

public static JSONObject doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost request = new HttpPost(url); 
     HttpEntity entity; 
     StringEntity s = new StringEntity(c.toString()); 

     s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
     entity = s; 
     request.setEntity(entity); 
     Log.v("entity",""+entity); 
     HttpResponse response; 
     try{ 
      response = httpclient.execute(request); 
      Log.v("REceiving","Received . . ."); 
      HttpEntity httpEntity = response.getEntity(); 
      is = httpEntity.getContent(); 
      Log.v("RESPONSE",""+is); 
     } 
     catch(Exception e){ 
      Log.v("Error in response",""+e.getMessage()); 
      } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      Log.v("Reader",""+reader.readLine()); 
      while ((line = reader.readLine()) != null) { 
       Log.v("line",""+line); 
       sb.append(line + "\n"); 
      } 
      Log.v("builder",""+sb); 
      is.close(); 
      json = sb.toString(); 

     } catch (Exception e) { 
      Log.v("Buffer Error", "Error converting result " + e.toString()); 
     } 

    // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.v("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 
    } 

}

我有PHP腳本:

$response = array(); 
$con=mysqli_connect("localhost","uname","password","db_manage"); 
if((isset($_POST['uname']) && isset($_POST['password']))){ 
$empid = $_POST['uname']; 

$pass = $_POST['password']; 
$query = "SELECT mm_emp_id,mm_password FROM employee_master WHERE mm_emp_id='$empid'and mm_password='$pass'"; 
$result = mysqli_query($con, $query); 
if(count($result) > 0){ 
    $response["success"] = 1; 
    $response["message"] = ""; 
    echo json_encode($response); 
} 
else{ 
    $response["success"] = 0; 
    $response["message"] = "The username/password does not match"; 
    echo json_encode($response); 
} 
} 

我是得到未定義索引在我檢查isset()的行。我在接收php腳本中的json時做錯了什麼?

如果你可以看到我已經使用link爲我的幫助 請幫助我。

+1

*旁註:*你的代碼受到SQL注入攻擊,因爲您直接允許將POST值插入到查詢中。 – Raptor

+0

@ShivanRaptor感謝您提供有價值的信息。我改變了這一點。但是我的phpscript沒有檢測到isset()值。你可以看看這個問題嗎?謝謝 :) – fastLearner

回答

0

在您不使用JSON對象(的JSONObject C)doPost方法包含變量

0
public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> { 

String url = "http://test.myhodo.in/index.php/test/execute"; 

@Override 
protected JSONObject doInBackground(JSONObject... data) { 
    JSONObject json = data[0]; 
    HttpClient client = new DefaultHttpClient(); 
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 

    JSONObject jsonResponse = null; 
    HttpPost post = new HttpPost(url); 
    try { 
     StringEntity se = new StringEntity("json="+json.toString()); 
     post.addHeader("content-type", "application/x-www-form-urlencoded"); 
     post.setEntity(se); 

     HttpResponse response; 
     response = client.execute(post); 
     String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 

     jsonResponse=new JSONObject(resFromServer); 
     Log.i("Response from server", jsonResponse.getString("msg")); 


    } catch (Exception e) { e.printStackTrace();} 

    return jsonResponse; 
} 

主要活動

try { 
       JSONObject toSend = new JSONObject(); 
       toSend.put("msg", "hello"); 

       JSONTransmitter transmitter = new JSONTransmitter(); 
       transmitter.execute(new JSONObject[] {toSend}); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
     }