2013-10-15 103 views
-1

我可以知道這個http連接有什麼問題嗎? 運行此操作時,我收到「HTTP問題」。android和數據庫之間的連接

public void makeHttpPost(){ 
    try 
    { 
     //http post 
     HttpPost httppost = new HttpPost("http://10.0.2.2/project/login.php");    
     HttpClient httpclient = new DefaultHttpClient();   
     httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } 
    catch(Exception e) 
    { 
     Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); 
     Toast.makeText(getBaseContext(),"HTTP PROBLEM",Toast.LENGTH_LONG).show(); 
    } 
} 
+0

u能logcat的?並且是運行在瀏覽器上的urph url? – KOTIOS

+0

這是錯誤顯示 - > android.os.NetworkOnMainThreadException php url cn運行 – Michelle

+0

好吧,這意味着你運行在OnCreate ur ur url而不是你需要異步任務,這將幫助你.. m後一個答案檢查out – KOTIOS

回答

1

調用下面的類,其中u HV稱爲makeHttpPost() 調用這個類如下:

new LoadData().execute(); 


    class LoadData extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 

     // ur http code 
      makeHttpPost(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 



    } 
} 
+0

我可以知道在哪裏添加緩衝區()和login()? – Michelle

+0

我想你登錄後緩衝權利?如果是,在doInBackground方法第一次火災登錄網址和火災url緩衝 – KOTIOS

+0

我直接緩衝區()和登錄()後makeHttpPost() 緩衝區()這裏的意思是將PHP的結果轉換爲字符串 – Michelle