-2

私はこのアンドロイドプログラミングのすべてに不慣れです。いくつかのコードを作成した後、ipString という次の文字列を取得しました。文字列をテキストビューに表示したいと思います。

ここに私が今持っているものがあります。

機能させるには何を追加すればよいですか?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("Rami","Rami");
    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("http://jsonip.com");
    // Depends on your web service
    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");

        }
        result = sb.toString();
        JSONObject jObject = new JSONObject(result);
        String ipString = jObject.getString("ip");
        Log.d("ip", ipString);
    } catch (Exception e) { 
        // Oops
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }

    TextView myTextView = (TextView) findViewById(R.id.textview1);
4

3 に答える 3

0

次のように setText() を使用します。

TextView myTextView = (TextView) findViewById(R.id.textview1);
myTextView.setText(ipString);

とにかく、これらの 2 行を try 内に配置する必要があります。そうすれば、HTTP リクエストが成功した場合にのみ文字列が出力されます。

try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");

        }
        result = sb.toString();
        JSONObject jObject = new JSONObject(result);
        String ipString = jObject.getString("ip");
        Log.d("ip", ipString);

        TextView myTextView = (TextView) findViewById(R.id.textview1);
        myTextView.setText(ipString);

    } catch (Exception e) { 
        // Oops
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }

ところで、ドキュメントを実際に読んで (そして理解しようとして)、IDE の使用方法を学ぶ必要があります。

于 2013-08-31T04:04:27.887 に答える
0

これを使用setText()すると、応答テキストが表示されます。

TextView myTextView = (TextView) findViewById(R.id.textview1);
myTextView.setText(ipString);
于 2013-08-31T03:49:11.987 に答える
0

次のように setText() メソッドを使用します。

String yourstringvalue;
TextView text = (TextView) findViewById(R.id.textviewID);

text.setText(yourstringvalue);
于 2013-08-31T06:52:49.250 に答える