2015-08-26 17 views
1

我想爲我的Android應用程序設置一個PHP API來進行交互,我的問題是發佈數據從未似乎發佈,我永遠不能檢索響應正文(HTML/TXT)從給定的URL。Android和PHP創建一個API

我的代碼

Thread thread = new Thread(new Runnable(){ 
     @Override 
     public void run() { 
      try { 
       HttpClient client = new DefaultHttpClient(); 
       HttpPost clientpost = new HttpPost("http://192.168.1.129/updateMain.php"); 

       try { 
        List<NameValuePair> params = new ArrayList<NameValuePair>(2); 

        params.add(new BasicNameValuePair("_id", "1")); 
        params.add(new BasicNameValuePair("job_name", "Test")); 

        clientpost.setEntity(new UrlEncodedFormEntity(params)); 

        HttpResponse response = client.execute(clientpost); 

        String responseBody = EntityUtils.toString(response.getEntity()); 

       } catch(Exception e) 
       { 
        Log.e("Error", e.toString()); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    thread.start(); 

如果我可以發佈數據,那麼我將能夠張貼JSON到服務器和從服務器獲取JSON,從而克服我目前的最大障礙。

任何幫助非常感謝。

+2

考慮使用Volley庫,它更快,更容易。 –

+0

我檢查了排除,再次迴應永遠不會被打印......這將是一個不同的問題,但它自己。感謝您的建議。 –

回答

1
public String getResponse(String url, List<NameValuePair> nameValuePairs) { 

    url = url.replaceAll(" ", "%20"); 
    String result = "ERROR"; 
    Log.d("Pair", nameValuePairs.toString()); 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpParams http_params = httpclient.getParams(); 
    http_params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, 
      HttpVersion.HTTP_1_1); 
    HttpConnectionParams.setConnectionTimeout(http_params, TIMEOUT); 
    HttpConnectionParams.setSoTimeout(http_params, TIMEOUT); 
    HttpResponse response = null; 

    try { 
     HttpPost httppost = new HttpPost(url); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     response = httpclient.execute(httppost); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     result = "ERROR"; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     result = "ERROR"; 
    } 

    try { 
     // Get hold of the response entity 
     HttpEntity entity = null; 
     if (response != null) { 
      entity = response.getEntity(); 
     } 
     if (entity != null) { 
      InputStream inputStream = entity.getContent(); 
      result = convertStreamToString(inputStream); 
     } 

    } catch (IOException e) { 
     result = "ERROR"; 
    } 

    httpclient.getConnectionManager().shutdown(); 
    return result; 
} 

使用此方法的AsyncTask或後臺線程

+0

只是打印錯誤,從不打印堆棧跟蹤。這種方法對於這樣一個簡單的任務似乎都很複雜?我很可能是錯的,但是需要付出很多努力。 –

1

這一個是爲我工作。請注意異常和錯誤處理。 Key121變量是你的php文件的url。

class callServiceTask extends AsyncTask<Void, Void, Void> 
{ 
    @Override 
    protected Void doInBackground(Void... params) { 
    loginCheck(); 
    return null; 
    } 
} 
public void loginCheck() 
{ 
InputStream is = null; 
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("aa","11")); 
nameValuePairs.add(new BasicNameValuePair("bb","22")); 

    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(KEY_121); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    }catch(Exception e) 
    { 
     Log.e("log_tag", "Error:"+e.toString()); 
    } 
//convert response to string 
    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(); 
    result=sb.toString(); 
    Log.e("log_tag", "----------------"+result); 
    }catch(Exception e){ 
    Log.e("log_tag", "Error converting result "+e.toString()); 
    } 
} 

您需要使用流和緩衝讀取器來分析響應。請檢查並告訴我們。

+0

不幸的是,對我而言,響應從未被記錄Log.d(「響應」,結果)從不打印任何東西。另外我也添加了catch,並在不同的線程中運行它。 –

+0

@JamesParker:答案中更新完整的課程代碼。檢查並讓我知道你的輸出/錯誤。 –

+0

在這裏,我會要求你的PHP文件,如你所說,即使其他方法不給出輸出。 –