Good day. The second day I suffer, I can not find what the problem is. I want to send a POST request, get a response and process it. I do everything in a separate thread, why it is impossible not to be clear.
package com.example.response; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button button1; private TextView info; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(button1Listener); info = (TextView)findViewById(R.id.textView1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public OnClickListener button1Listener = new OnClickListener() { @Override public void onClick(View v) { DoPOST dp = new DoPOST(); dp.doInBackground("http://www.forismatic.com/api/1.0/"); } }; private class DoPOST extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { super.onPreExecute(); info.setText("Start"); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); info.setText("All is right"); } @Override protected String doInBackground(String... sUrl) { String citateRequestString = "http://www.forismatic.com/api/1.0/"; String resultString = new String(""); try { URLConnection connection = null; URL url = new URL(citateRequestString); // если используется прокси // int portOfProxy = android.net.Proxy.getDefaultPort(); if( portOfProxy > 0 ){ Proxy proxyDef = new Proxy(Proxy.Type.HTTP,new InetSocketAddress( android.net.Proxy.getDefaultHost(), portOfProxy)); connection = url.openConnection(proxyDef); } else { connection = url.openConnection( ); } ///////////////////////////// HttpURLConnection httpConnection = (HttpURLConnection)connection; httpConnection.setRequestMethod("POST"); httpConnection.setRequestProperty("User-Agent", "MyAndroid/1.6"); httpConnection.setRequestProperty("Content-Language", "ru-RU"); httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpConnection.setDoOutput(true); httpConnection.setDoInput(true); httpConnection.connect(); // здесь можем писать в поток данные запроса OutputStream os = httpConnection.getOutputStream(); String str = "method=getQuote&key=999&format=text&lang=ru"; os.write( str.getBytes()); os.flush(); os.close(); int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream in = httpConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(in, "UTF-8"); StringBuffer data = new StringBuffer(); int c; while ((c = isr.read()) != -1){ data.append((char) c); } resultString = new String (data.toString()); } else { resultString = "Server does not respond"; } } catch (MalformedURLException e) { resultString = "MalformedURLException:" + e.getMessage(); } catch (IOException e) { resultString = "IOException:" + e.getMessage(); } info.setText("resultString"); return resultString; } }; }
Logs:
03-02 09:33:31.577: D/dalvikvm(876): Late-enabling CheckJNI 03-02 09:33:32.198: W/ActivityThread(876): Application com.example.response is waiting for the debugger on port 8100... 03-02 09:33:32.208: I/System.out(876): Sending WAIT chunk 03-02 09:33:33.579: I/dalvikvm(876): Debugger is active 03-02 09:33:33.609: I/System.out(876): Debugger has connected 03-02 09:33:33.609: I/System.out(876): waiting for debugger to settle... 03-02 09:33:33.809: I/System.out(876): waiting for debugger to settle... 03-02 09:33:34.009: I/System.out(876): waiting for debugger to settle... 03-02 09:33:34.210: I/System.out(876): waiting for debugger to settle... 03-02 09:33:34.410: I/System.out(876): waiting for debugger to settle... 03-02 09:33:34.610: I/System.out(876): waiting for debugger to settle... 03-02 09:33:34.810: I/System.out(876): waiting for debugger to settle... 03-02 09:33:35.010: I/System.out(876): waiting for debugger to settle... 03-02 09:33:35.211: I/System.out(876): waiting for debugger to settle... 03-02 09:33:35.411: I/System.out(876): waiting for debugger to settle... 03-02 09:33:35.611: I/System.out(876): waiting for debugger to settle... 03-02 09:33:35.821: I/System.out(876): waiting for debugger to settle... 03-02 09:33:36.021: I/System.out(876): waiting for debugger to settle... 03-02 09:33:36.222: I/System.out(876): waiting for debugger to settle... 03-02 09:33:36.422: I/System.out(876): waiting for debugger to settle... 03-02 09:33:36.632: I/System.out(876): waiting for debugger to settle... 03-02 09:33:36.832: I/System.out(876): waiting for debugger to settle... 03-02 09:33:37.032: I/System.out(876): waiting for debugger to settle... 03-02 09:33:37.233: I/System.out(876): waiting for debugger to settle... 03-02 09:33:37.433: I/System.out(876): waiting for debugger to settle... 03-02 09:33:37.633: I/System.out(876): waiting for debugger to settle... 03-02 09:33:37.833: I/System.out(876): waiting for debugger to settle... 03-02 09:33:38.033: I/System.out(876): waiting for debugger to settle... 03-02 09:33:38.234: I/System.out(876): debugger has settled (1473) 03-02 09:33:40.175: D/libEGL(876): loaded /system/lib/egl/libGLES_android.so 03-02 09:33:40.195: D/libEGL(876): loaded /system/lib/egl/libEGL_adreno200.so 03-02 09:33:40.306: D/libEGL(876): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 03-02 09:33:40.306: D/libEGL(876): loaded /system/lib/egl/libGLESv2_adreno200.so 03-02 09:33:40.446: D/memalloc(876): /dev/pmem: Mapped buffer base:0x520c9000 size:2088960 offset:0 fd:63 03-02 09:33:40.516: D/OpenGLRenderer(876): Enabling debug mode 0 03-02 09:33:41.146: D/memalloc(876): /dev/pmem: Mapped buffer base:0x52676000 size:24997888 offset:22908928 fd:66 03-02 09:33:42.468: D/memalloc(876): /dev/pmem: Mapped buffer base:0x53e4d000 size:27086848 offset:24997888 fd:69 03-02 09:33:42.558: D/AndroidRuntime(876): Shutting down VM 03-02 09:33:42.558: W/dalvikvm(876): threadid=1: thread exiting with uncaught exception (group=0x40ab2228) 03-02 09:33:42.638: E/AndroidRuntime(876): FATAL EXCEPTION: main 03-02 09:33:42.638: E/AndroidRuntime(876): android.os.NetworkOnMainThreadException 03-02 09:33:42.638: E/AndroidRuntime(876): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) 03-02 09:33:42.638: E/AndroidRuntime(876): at java.net.InetAddress.lookupHostByName(InetAddress.java:394) 03-02 09:33:42.638: E/AndroidRuntime(876): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245) 03-02 09:33:42.638: E/AndroidRuntime(876): at java.net.InetAddress.getAllByName(InetAddress.java:220) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 03-02 09:33:42.638: E/AndroidRuntime(876): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 03-02 09:33:42.638: E/AndroidRuntime(876): at com.example.response.MainActivity$DoPOST.doInBackground(MainActivity.java:116) 03-02 09:33:42.638: E/AndroidRuntime(876): at com.example.response.MainActivity$1.onClick(MainActivity.java:63) 03-02 09:33:42.638: E/AndroidRuntime(876): at android.view.View.performClick(View.java:3549) 03-02 09:33:42.638: E/AndroidRuntime(876): at android.view.View$PerformClick.run(View.java:14393) 03-02 09:33:42.638: E/AndroidRuntime(876): at android.os.Handler.handleCallback(Handler.java:605) 03-02 09:33:42.638: E/AndroidRuntime(876): at android.os.Handler.dispatchMessage(Handler.java:92) 03-02 09:33:42.638: E/AndroidRuntime(876): at android.os.Looper.loop(Looper.java:154) 03-02 09:33:42.638: E/AndroidRuntime(876): at android.app.ActivityThread.main(ActivityThread.java:4945) 03-02 09:33:42.638: E/AndroidRuntime(876): at java.lang.reflect.Method.invokeNative(Native Method) 03-02 09:33:42.638: E/AndroidRuntime(876): at java.lang.reflect.Method.invoke(Method.java:511) 03-02 09:33:42.638: E/AndroidRuntime(876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-02 09:33:42.638: E/AndroidRuntime(876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-02 09:33:42.638: E/AndroidRuntime(876): at dalvik.system.NativeStart.main(Native Method)