Reputation: 1737
I have one small android app which insert data to table using MySQL (via php site). Everything works fine when I'm using wifi but when I disable wifi data is not stored in database. Here is my code which I use to connect to my local php file:
protected String doInBackground(Void... params) {
String url = "http://192.168.1.2:6969/android/index.php";
try {
URL address = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) address.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String postData = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&"
+ URLEncoder.encode("emailAddress", "UTF-8") + "=" + URLEncoder.encode(emailAddress, "UTF-8");
bufferedWriter.write(postData);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String line;
while((line = bufferedReader.readLine())!=null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (IOException e) {
}
return null;
}
I discovered that the problem is with line:
OutputStream outputStream = httpURLConnection.getOutputStream();
My app stops there.
Is there a way to force this application to work without wifi connection?
Here is logcat when I start my app on debug usb mode on my android phone:
E/GED: Failed to get GED Log Buf, err(0)
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
D/libc-netbsd: [getaddrinfo]: mtk netid=0; mark=0
D/libc-netbsd: getaddrinfo( app_uid:10226
D/libc-netbsd: getaddrinfo() uid prop:
D/libc-netbsd: getaddrinfo() getuid():10226
D/libc-netbsd: [getaddrinfo]: mtk ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
D/libc-netbsd: [getaddrinfo]: mtk netid=0; mark=0
D/libc-netbsd: getaddrinfo( app_uid:10226
D/libc-netbsd: getaddrinfo() uid prop:
D/libc-netbsd: getaddrinfo() getuid():10226
D/libc-netbsd: [getaddrinfo]: mtk ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
Upvotes: 0
Views: 770
Reputation: 104
I would suggest you to use a library that handles everything for you.
Like http://square.github.io/okhttp/
Here is an example code how it works for URL-Encoded Requests:
RequestBody formBody = new FormBody.Builder()
.add("name", "name")
.add("emailAdress", "[email protected]")
.build();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
And a more important thing is. You are having a 192.168.x.x IP-Address. This is used for a private network and is not available through public Internet. If you know your Routers public IP you could make a forwarding to your PC, but i would not suggest that to you.
Upvotes: 1