NetworkingSocketsActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class NetworkingSocketsActivity extends Activity { TextView mTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView) findViewById(R.id.textView1); final Button loadButton = (Button) findViewById(R.id.button1); loadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new HttpGetTask().execute(); } }); } private class HttpGetTask extends AsyncTask<Void, Void, String> { private static final String HOST = "api.geonames.org"; // Get your own user name at http://www.geonames.org/login private static final String USER_NAME = "indrawdev"; private static final String HTTP_GET_COMMAND = "GET /earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=" + USER_NAME + " HTTP/1.1" + "\n" + "Host: " + HOST + "\n" + "Connection: close" + "\n\n"; private static final String TAG = "HttpGet"; @Override protected String doInBackground(Void... params) { Socket socket = null; String data = ""; try { socket = new Socket(HOST, 80); PrintWriter pw = new PrintWriter(new OutputStreamWriter( socket.getOutputStream()), true); pw.println(HTTP_GET_COMMAND); data = readStream(socket.getInputStream()); } catch (UnknownHostException exception) { exception.printStackTrace(); } catch (IOException exception) { exception.printStackTrace(); } finally { if (null != socket) try { socket.close(); } catch (IOException e) { Log.e(TAG, "IOException"); } } return data; } @Override protected void onPostExecute(String result) { mTextView.setText(result); } private String readStream(InputStream in) { BufferedReader reader = null; StringBuffer data = new StringBuffer(); try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { data.append(line); } } catch (IOException e) { Log.e(TAG, "IOException"); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { Log.e(TAG, "IOException"); } } } return data.toString(); } } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/load_data_string" > </Button> <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" > </TextView> </ScrollView> </LinearLayout> |