[Wear OS Q&A, Help & Troubleshooting] yjay18:
I'm trying to build a simple app that sends data to Microsoft Azure IoT Central but I'm getting W/System.err: android.os.NetworkOnMainThreadException error.

Here is my code:

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends WearableActivity implements View.OnClickListener {

Button clickMeButton;
TextView textView;
int count = 0;

private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.click_button);
button.setOnClickListener(this);

setAmbientEnabled();
}

@Override
public void onClick(View view) {
count++;
final TextView text = (TextView) findViewById(R.id.results);
text.setText("I am clicked: "+count);
int tempValue = count;
JSONObject json = new JSONObject();
try {
json.put("Count", count);
} catch (JSONException e) {
e.printStackTrace();
}
// @Deprecated HttpClient httpClient = new DefaultHttpClient();
HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("URL Link");
StringEntity params = new StringEntity(json.toString());
request.addHeader("Authorization", "SharedAccessSignature Token");
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = (HttpResponse) httpClient.execute(request);
} catch (Exception ex) {
} finally {
// @Deprecated httpClient.getConnectionManager().shutdown();
Log.e("Hello", "I'm done running");
}

Here is the trace:

W/System.err: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
at java.net.InetAddress.getAllByName(InetAddress.java:1154)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:259)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at com.example.wearazure.MainActivity.onClick(MainActivity.java:97)
at android.view.View.performClick(View.java:6599)
at android.view.View.performClickInternal(View.java:6576)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25908)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Any suggestions are appreciated, thanks!