The program must connect to the database, I decided to do it through Json. However, during startup it gives:

1259-1259/com.example.valera.homeweatherstation E/AndroidRuntime﹕ FATAL EXCEPTION: main android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) at libcore.io.IoBridge.connectErrno(IoBridge.java:127) at libcore.io.IoBridge.connect(IoBridge.java:112) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) at java.net.Socket.connect(Socket.java:842) at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) at com.example.valera.homeweatherstation.parser.JSONParser.makeHttpRequest(JSONParser.java:53) at com.example.valera.homeweatherstation.MySqlReader.AllProductsActivity$GetDataFromMySQL$1.run(AllProductsActivity.java:60) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 

The code itself (made through AsyncTask, in the manifest <uses-permission android:name="android. permission.INTERNET"/> spelled):

 public class AllProductsActivity extends ActionBarActivity { TextView t_inside_text; TextView t_outside_text; TextView Pressure_text; TextView Humidity_text; JSONParser jsonParser = new JSONParser(); private static String url_get_data = "<ТУТ IP>"; private static final String Success = "success"; private static final String Data = "data"; private static final String t_inside = "t_inside"; private static final String t_outside = "t_outside"; private static final String Pressure = "Pressure"; private static final String Humidity = "Humidity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new GetDataFromMySQL().execute(); } class GetDataFromMySQL extends AsyncTask<String, String, String> { protected String doInBackground(String[] params) { runOnUiThread(new Runnable() { public void run() { int success; try { List<NameValuePair> params = new ArrayList<NameValuePair>(); JSONObject json = jsonParser.makeHttpRequest(url_get_data, "GET", params); Log.d("Single Product Details", json.toString()); success = json.getInt(Success); if (success == 1) { JSONArray productObj = json.getJSONArray(Data); JSONObject product = productObj.getJSONObject(0); t_inside_text = (TextView) findViewById(R.id.t_inside); t_outside_text = (TextView) findViewById(R.id.t_outside); Pressure_text = (TextView) findViewById(R.id.Pressure); Humidity_text = (TextView) findViewById(R.id.Humidity); t_inside_text.setText(product.getString(t_inside) + " °C"); t_outside_text.setText(product.getString(t_outside) + " °C"); Pressure_text.setText(product.getString(Pressure) + " мм.рт.столба"); Humidity_text.setText(product.getString(Humidity) + " %"); } catch (JSONException e) { e.printStackTrace(); } } }); return null; } } } 

And JSONParser:

 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public JSONParser() { } public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { try { if(method == "POST"){ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); }else if(method == "GET"){ DefaultHttpClient httpClient = new DefaultHttpClient(); String paramString = URLEncodedUtils.format(params, "utf-8"); url += "?" + paramString; HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // возвращаем JSON строку return jObj; } } 
  • @Dumpling, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

2 answers 2

(yawning) you have android.os.NetworkOnMainThreadException . Remove work with the network from the main stream.

 class GetDataFromMySQL extends AsyncTask<String, String, String> { protected String doInBackground(String[] params) { runOnUiThread(new Runnable() { public void run() { ... //создание сетевого подключения и получение инфы по сети } 

This I have not seen ...

 com.example.valera - забавно. 

    You are running AsyncTask code on the main thread. WHAT FOR??? What is this terrible bike of crutches?
    You use AsyncTask to get the network to work from the main thread, but inside you use runOnUiThread to execute code on the main thread. It's like in a cartoon about Cheburashka and Gena. "Gene, let me carry the bags, and you carry me."