我有一个小的Android应用程序,使用MySQL(通过php网站)将数据插入到表中。当我使用wifi时,一切正常,但是当我禁用wifi时,数据不存储在数据库中。这是我用来连接到我的本地php文件的代码:
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;
}
我发现问题在于行:
OutputStream outputStream = httpURLConnection.getOutputStream();
我的应用停在那里。
有没有办法强制这个应用程序在没有wifi连接的情况下工作?
当我在Android手机上调试usb模式启动我的应用程序时,这是logcat:
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
答案 0 :(得分:1)
我建议你使用一个为你处理一切的库。
像 http://square.github.io/okhttp/
以下是一个示例代码,它如何用于URL编码请求:
RequestBody formBody = new FormBody.Builder()
.add("name", "name")
.add("emailAdress", "xxx@xxx.com")
.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();
更重要的是。您有一个192.168.x.x IP地址。这用于专用网络,并且通过公共互联网不可用。如果您知道您的路由器公共IP,您可以转发到您的PC,但我不建议您。