0

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

这是我的活动:

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 发布到它时,PHP 就可以工作。我假设这条线

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

以相同的方式发布到 php,因为 IP 应该是在 android 上输入到文本框中的内容,并且它与 PHP 中的 POST 匹配。

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

4

1 回答 1

1

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

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

于 2012-07-11T16:35:11.573 回答