2012-01-30 35 views
0

我對編程非常陌生,並且在本網站上有很多人的幫助,我已經能夠取得很多進展。這一次我有點被卡住了 - 我已經能夠登錄屏幕並撥打電話,但一旦撥打電話就沒有任何迴應。在eclipse中顯示來自url的響應

如果任何人都可以幫助,我將不勝感激,再次我很新,所以,請,任何額外的代碼將有所幫助。這是我迄今爲止所擁有的。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Loginretrieve extends Activity { 

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

    String url = "http://beatswith.us/login.php"; 

    HttpGet httpGet = new HttpGet(url); 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpResponse response; 
    try { 

     response = httpClient.execute(httpGet); 

     Object responseString = getResponseString(response); 

    } catch (ClientProtocolException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } 


} 

public static String getResponseString(HttpResponse response) 
     throws IllegalStateException, IOException { 

    String responseString = ""; 
    BufferedReader in = null; 
    try { 
     in = new BufferedReader(new InputStreamReader(response.getEntity() 
       .getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 
     responseString = sb.toString(); 

    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    return responseString; 
} 
} 
+0

在getResponseString方法的末尾(return語句之前),添加System.out並打印'responseString'。哪個應該給你響應字符串。 – kosa 2012-01-30 16:47:04

+0

在try塊中嘗試使用'Log.d(「tag」,response)'格式進行日誌記錄,以記錄response和responseString並檢查那裏的輸出(在logcat窗口中)... – 2012-01-30 17:39:16

+0

所以即使我的日誌貓沒有任何錯誤在它的只是沒有顯示任何東西在屏幕上只是回來黑色 – trunks85719 2012-01-31 21:36:44

回答

0

您是否在logcat中收到任何錯誤消息?

這絕對有助於確定問題。您可能會錯過USES_PERMISSION INTERNET,或者根據您使用的android版本,可能是因爲嘗試在UI線程上執行網絡調用或其他一些問題。

另外,如果你要打印到控制檯,這樣做是爲了在logcat的搭配:

Log.d("YourAppTag", "Output"); 

然後將在普通的Java應用程序的logcat很像控制檯顯示出來。

編輯:如果您不確定如何進入logcat,請在Eclipse中轉到Window-> Show View-> Other。然後在Android下選擇Logcat。

+0

所以我沒有得到任何錯誤在我的日誌貓所有它只是不顯示任何東西在屏幕上只是回來黑 – trunks85719 2012-01-31 20:15:10