I have created API service in ASP.NET. I have configured it to make it accessible from another devices in the same LAN. I am sure it works because I am able to visit my website from Android device or emulator. The problem is that I can't call this API from my app in both Android physical device and emulator. I am using "http:\192.168.1.133:2920\api\notes\create" address on device and "http:\10.0.2.2:2920\api\notes\create" on emulator. I have configured also firewall on PC. I know it passes connection because I can execute request from Android web browser. I am sure that the address which I am using in app is 100% correct (I've checked it in debugger). I have also set breakpoint in ASP.NET to see if the action in controller was called, but wasn't. Here is the code from my Android app:
public void sendCommand(final Command command)
{
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection urlConnection;
String data = command.getJson();
//Connect
urlConnection = (HttpURLConnection) ((new URL(_serverAddress + command.getUri()).openConnection()));
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
//Write
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(data);
writer.close();
outputStream.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("sendCommand", e.getMessage());
return;
}
}
});
thread.start();
}
Of course I have added required permissions. Here is my AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="test.notes">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NoteActivity"></activity>
</application>
</manifest>
Aucun commentaire:
Enregistrer un commentaire