我正在尝试从服务器获取回显回复。这样做的目的是检查Internet连接并检查服务器状态。如果我得到答复,将出现一个对话框,其中包含一条简单消息。没有得到答复也是如此。
该对话框在收到回复时出现,但在没有收到回复时不会出现。
在我的uploadDB.class中,我有多种情况将在PHP文件中执行不同的功能。一种情况是“ ping”功能,另一种情况是“ updatecounter”功能。
当我在uploadDB.class中删除“ updatecounter”案例时,一切似乎都可以正常工作。根据我的要求,我不应该删除“ updatecounter”案例。
有人可以建议我如何解决这个问题吗?
下面是我在Logcat中的代码和错误。
PHP文件
if (isset($_POST['type'])){
if ($_POST['type'] == "ping"){
echo "success";
}
MainActivity.class
public boolean isInternetConnected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pingTask();
}
private void pingTask() {
new uploadDB(new uploadDB.returnResult() {
@Override
public void onFinish(String result) {
if (result.equalsIgnoreCase("success")) {
isInternetConnected = true;
showdialog("Server Status", "Working");
} else {
isInternetConnected = false;
showdialog("Server Status", "Not Working");
}
}
}).execute("ping");
}
uploadDB.class
protected String doInBackground(String... params) {
String method = params[0];
String data = "";
String link = "www.example.com/example.php";
switch (method){
case "ping": {
try{
data = URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode("ping", "UTF-8");
Log.e(TAG,"DATA: " + data);
}catch (UnsupportedEncodingException e){
Log.e(TAG, "UnsupportedEncodingException: " + e.toString());
}
try {
URL url = new URL(link);
HttpURLConnection h = (HttpURLConnection) url.openConnection();
h.setDoInput(true);
h.setDoOutput(true);
OutputStreamWriter owriter =
new OutputStreamWriter(h.getOutputStream());
owriter.write(data);
owriter.flush();
BufferedReader b = new BufferedReader
(new InputStreamReader(h.getInputStream(), "UTF-8"), 8);
StringBuilder builder = new StringBuilder();
String line;
while ((line = b.readLine()) != null) {
builder.append(line);
}
return builder.toString();
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
}
}
case "updatecounter": {
String macAddress = params[1];
String sensorname = params[2];
String counter = params[3];
try {
data = URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode("counter", "UTF-8")
+ "&" + URLEncoder.encode("gate_name", "UTF-8") + "=" + URLEncoder.encode(sensorname, "UTF-8")
+ "&" + URLEncoder.encode("mac", "UTF-8") + "=" + URLEncoder.encode(macAddress, "UTF-8")
+ "&" + URLEncoder.encode("counter", "UTF-8") + "=" + URLEncoder.encode(counter, "UTF-8");
Log.e(TAG,"DATA: " + data);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UnsupportedEncodingException: " + e.toString());
}
try {
URL url = new URL(link);
HttpURLConnection h = (HttpURLConnection) url.openConnection();
h.setDoInput(true);
h.setDoOutput(true);
OutputStreamWriter owriter =
new OutputStreamWriter(h.getOutputStream());
owriter.write(data);
owriter.flush();
BufferedReader b = new BufferedReader
(new InputStreamReader(h.getInputStream(), "UTF-8"), 8);
StringBuilder builder = new StringBuilder();
String line;
while ((line = b.readLine()) != null) {
builder.append(line);
}
return builder.toString();
} catch (IOException e) {
//activity.isInternetConnected = false;
Log.e(TAG, "IOException: " + e.toString());
}
}
Logcat错误
Process: com.example.postserver, PID: 21624
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.example.postserver.uploadDB.doInBackground(uploadDB.java:69)
at com.example.postserver.uploadDB.doInBackground(uploadDB.java:16)
答案 0 :(得分:3)
您需要在案例末尾添加一个休息点。如果您不添加它,则此后的每种情况都将执行:
PropertyName =
答案 1 :(得分:-1)
您可以从android studio服务器中获得服务器的任何响应
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String dataResponse = "";
String inputLine = "";
while((inputLine = bufferedReader.readLine()) != null){
dataResponse += inputLine;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
之后,您可以打印出您的回复
System.out.println(dataResponse);
在您的邮政编码后放置该代码