0

The problem is when I enter a string or a name in the edittext, it cannot be appeared in the textview after i press the button "Send request". I don't know how to fix it.
Please help me!

package com.androidexample.httpgetexample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

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 org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HttpGetAndroidExample extends Activity {

    TextView content;
    EditText userID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_get_android_example);

        content = (TextView) findViewById(R.id.content);
        userID = (EditText) findViewById(R.id.user);
        Button saveme = (Button) findViewById(R.id.save);

        saveme.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                // ALERT MESSAGE

                Toast.makeText(getBaseContext(),
                        "Please wait, connecting to server.", Toast.LENGTH_LONG)
                        .show();
                try {

                    String userIDValue = URLEncoder.encode(userID.getText().toString(), "UTF-8");
                    // HttpClient Client = new DefaultHttpClient();
                    String URL = "http://140.113.241.10:9000/request?userID="
                            + userIDValue;


                    new HttpAsyncTask().execute(URL);

                } catch (UnsupportedEncodingException ex) {
                    content.setText("Fail");
                }
            }
        });
    }

    public static String GET(String url) {
        InputStream inputStream = null;
        String response = new String(); 
        // result="";
        try {

            if (inputStream != null)
                response = convertInputStreamToString(inputStream);

            else
                response = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return response;
    }

    private static String convertInputStreamToString(InputStream inputStream)
            throws IOException {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
        String line = "";
        // String result = "";
        StringBuilder result = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null)
             result.append(line + "\n");

        inputStream.close();
        return result.toString();

    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            return GET(urls[0]);
        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG)
                    .show();
            content.setText(result);

            // String type = mainObject.getString("capsuleType");
        }
    }
}

Edited by thuyson

2
Contributors
1
Reply
9
Views
3 Years
Discussion Span
Last Post by peter_budo
0

Well either you posted wrong code piece or you did not implemented this yet and trying to trick us in doing your work.

Either way there is no send button and no action associated with it to do what you ask to do

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.