2012-12-18 3 views
0

이 문제에 관해 많은 질문이 있었지만 혼란스럽고 문제에 대한 해결책을 찾을 수 없다는 것을 알고 있습니다.웹 서비스 용 안드로이드 HttpReponse의 오류

나는 mysql 서버에서 데이터를 읽는 PHP 웹 서비스를 가지고있다. 다음 코드는 웹 서비스 스크립트입니다.

<?php 
$username = "XXXXX"; 
$password = "XXXXX"; 
$hostname = "localhost"; 

$response=array(); 

//connection to the database 
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL"); 


//select a database to work with 
$selected = mysql_select_db("test",$dbhandle) 
or die("Could not select test"); 

//execute the SQL query and return records 
$result = mysql_query("SELECT name, country FROM android"); 

$response["infos"] = array(); 

while ($row = mysql_fetch_assoc($result)) { 

    $info = array(); 
    $info["name"]=$row["name"]; 
    $info["country"]=$row["country"]; 

    echo json_encode($info); 
    } 

//close the connection 
mysql_close($dbhandle); 
    ?> 

이 나에게이 함께 웹 브라우저에서 다음과 같은 출력 제공에 "http : //localhost/test.php"{ {: "TAZ", "국가", "AUS" "이름"} "name": "fufu", "country": "tutu"} { "name": "chikaka", "country": "aceVentura"}

다음 안드로이드 응용 프로그램에 다음과 같은 클래스가 있습니다. 매우 간단한 클래스이지만 HttpResponse 실행시 예외 오류를 반환합니다.

public static final String URL = "http://192.168.0.5//test.php"; 

public static String connect(){ 

    try{ 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(URL); 
     HttpResponse response = httpClient.execute(httpGet); 

     String result = ""; 
     InputStream in = response.getEntity().getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     StringBuilder str = new StringBuilder(); 
     String line = null; 
     while((line = reader.readLine()) != null){ 
      str.append(line + "\n"); 
     } 
     in.close(); 
     result = str.toString(); 

     return result; 
    } catch (Exception e) { 
     return "Error: " + e.getMessage(); 
    } 
    } 

이 메서드는 응답을 출력하기 위해 mainActivity 클래스에서 호출됩니다. 내가 잘못하고있는 것에 대해 아이디어를 줄 수 있을까요?

먼저 나는 해결책을 발견 한의 InputStream

int statusCode = response.getStatusLine().getStatusCode(); 
if (statusCode == HttpStatus.SC_OK) { 
    HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
     return entity.getContent(); 
    } 
} 
+0

예외를 위해 logcat 행을 게시 할 수 있습니까? – Yogesh

+0

귀하의 웹 서비스 링크가 로컬이고 서버에 전송한다고 생각합니다. – QuokMoon

+0

이 예외가 있습니다. android.os.NetworkOnMainThreadException – mokko211

답변

0

는 응답 상태를 읽을 수 있는지 확인 감사드립니다. 그것은 매우 어리 석었습니다. 위치를 옮겼고 서버 주소가 변경된 것을 알지 못했습니다.

그럼에도 불구하고 도움을 주셔서 감사합니다.

+0

이 코드 조각을 포함 시켰지만 예외는 httpGet의 실행에서만 발생합니다. 그래서 다음 줄에서 : HttpResponse response = httpClient.execute (httpGet); – mokko211

0

죄송 사람 을 읽기 전에 당신이