2013-06-18 212 views
0

我試圖開發一個Android應用程序。在這個應用程序中,我需要發送一個POST請求到一個PHP頁面。Android POST請求到PHP

我的代碼是在Java方面:

DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://localhost/get.php"); 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("mail", "asdasd")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    try{ 
      HttpResponse httpresponse = httpclient.execute(httppost); 

    }catch(Exception e){ 
     Toast.makeText(getApplicationContext(), "Didn't Happen",10).show();  
    } 

的Manifest.xml

<? 
xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.misman" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:permission="android.permission.INTERNET" 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.misman.MainActivity" 
      android:label="@string/app_name" > 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我已經從很多網站搜索和代碼放在catch塊continuously.I也使用HttpURLConnection類和沒有工作。你能幫助我解決問題嗎?

+0

培訓,您應該記錄異常,而不是隻打印「沒有發生「在捕獲b鎖。這將幫助您調試問題。 – 1615903

回答

0

我的問題是,我想在應用程序主線程中運行我的代碼,主線程不允許這樣做。有幾個執行後臺操作

  • 在一個新的線程
  • MainActivity啓用網絡操作運行代碼的方式來解決這個問題,

    1. 使用AsyncTask,這就是我現在的選擇

      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder(). 
                     detectNetwork().build(); 
      StrictMode.setThreadPolicy(policy); 
      

    UPDATE

    由於問題的名稱,我想分享如何在android中進行Http調用以引導人們。有一些API:

    並且在

  • 4

    使用下面的代碼。它工作正常

    public class HttpClient { 
        private static final String TAG = "HttpClient"; 
    
        public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) { 
    
        try { 
         DefaultHttpClient httpclient = new DefaultHttpClient(); 
         HttpPost httpPostRequest = new HttpPost(URL); 
    
         StringEntity se; 
         se = new StringEntity(jsonObjSend.toString()); 
    
         httpPostRequest.setEntity(se); 
         httpPostRequest.setHeader("Accept", "application/json"); 
         httpPostRequest.setHeader("Content-type", "application/json"); 
         httpPostRequest.setHeader("Accept-Encoding", "gzip"); 
    
         long t = System.currentTimeMillis(); 
         HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); 
         Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]"); 
    
         HttpEntity entity = response.getEntity(); 
    
         if (entity != null) { 
          InputStream instream = entity.getContent(); 
          Header contentEncoding = response.getFirstHeader("Content-Encoding"); 
          if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { 
           instream = new GZIPInputStream(instream); 
          } 
    
          String resultString= convertStreamToString(instream); 
          instream.close(); 
          resultString = resultString.substring(0,resultString.length()-1); 
    
          JSONObject jsonObjRecv = new JSONObject(resultString); 
          Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>"); 
    
          return jsonObjRecv; 
         } 
    
        } 
        catch (Exception e) 
        { 
         Log.e("Exception", "Exception"); 
         e.printStackTrace(); 
        } 
        return null; 
    } 
    
    
    private static String convertStreamToString(InputStream is) { 
        BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
        StringBuilder sb = new StringBuilder(); 
    
        String line = null; 
        try { 
         while ((line = reader.readLine()) != null) { 
          sb.append(line + "\n"); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } finally { 
         try { 
          is.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
        return sb.toString(); 
        } 
    }