2012-07-11 122 views
0

我有我的Android模拟器暂时连接到本地计算机。它联系PHP并发布要运行的IP地址。然后它回应一个JSON结果。我想打印结果,但无法让android屏幕更改。在Android屏幕上打印JSON响应

这里是我的活动:

public class MainActivity extends Activity implements OnClickListener { 

EditText ipAddress; 
Button bSearch; 

String IP; 

HttpClient httpclient; 

HttpPost httppost; 

ArrayList<NameValuePair> nameValuePairs; 

HttpResponse response; 
HttpEntity entity; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    initialize(); 
    } 

private void initialize(){ 
    ipAddress = (EditText) findViewById(R.id.IPaddress); 
    bSearch = (Button) findViewById(R.id.searchBtn); 

    bSearch.setOnClickListener((OnClickListener) this); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

    httpclient = new DefaultHttpClient(); 

    httppost = new HttpPost("http://127.0.0.1/myfiles/WorkingVersionVJSON.php"); 

    IP = ipAddress.getText().toString(); 

    nameValuePairs = new ArrayList<NameValuePair>(); 

    nameValuePairs.add(new BasicNameValuePair("ipaddress", IP)); 

    try{ 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     response = httpclient.execute(httppost); 

     if(response.getStatusLine().getStatusCode()==200){ 

      entity = response.getEntity(); 

      if(entity != null){ 

       InputStream instream = entity.getContent(); 

       JSONObject jsonResponse = new JSONObject(convertStreamToString(instream)); 

       String rIP = jsonResponse.getString("ipaddress"); 

      } 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

private static String convertStreamToString(InputStream is) { 
     /* 
     * To convert the InputStream to String we use the BufferedReader.readLine() 
     * method. We iterate until the BufferedReader return null which means 
     * there's no more data to read. Each line will appended to a StringBuilder 
     * and returned as String. 
     */ 
     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(); 
    } 
} 

而且PHP

<?php 



require("IPQFunctionworkingversionV7.php"); 
$ipaddress = $_POST["ipaddress"]; 

$results = array(); 

$results = getScore($ipaddress); 
echo json_encode($results); 
?> 

PHP的工作时,我在浏览器中运行它,并使用HTML张贴到它。我假定线

nameValuePairs.add(new BasicNameValuePair("ipaddress", IP)); 

帖子到PHP以同样的方式,因为IP应该是什么的进入对Android的文本框,它的POST在PHP相匹配。

活动编译并位于模拟器上,所以我假定连接成功。我只需要以能够让它显示在屏幕上的方式解析JSON响应。一旦我有了,我会在布局上工作。

回答

1

此外,您在onClick中的代码确实需要放置在AsyncTask中。 OnClick从UI线程中调用。如果您在进行网络通话时间过长(长度不确定),Android会向您的用户发布该应用无响应。

要获得屏幕上的字符串,您的布局需要定义一个TextView,您可以通过findViewById获取并调用setText,或者通过以编程方式膨胀一个TextView并将其添加到显示中。