在调试中,我可以看到它把我需要的所有东西都放到了sb StringBuilders中,但是在第二个sb之后,数据消失了,以前写到safety_string中的内容也被删除了,不知道为什么。
这是整个代码,自从我发布以来,对其进行了一些更改。 仍然一样,这也不超出范围,因为我收到的getting_kat结果只是丢失了getting_ter结果。
@Override
public String doInBackground(String... voids) {
String result="";
result=getting_kat();
result+=getting_ter();
//Making sure it gets put into the safety string
safety_string=result;
return result;
}
public String getting_kat(){
String result="";
String resultkat = null;
String connstr = "My connection string";
try {
URL url = new URL(connstr);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
InputStream ips = http.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = "";
//Line by Line reading the data from php
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
//String it together into one line
resultkat = sb.toString();
//Adding a separator for later split
result=resultkat+"#KATEGORIA/TERMEK#";
br.close();
ips.close();
http.disconnect();
} catch (MalformedURLException e) {
result = e.getMessage();
} catch (IOException e) {
result = e.getMessage();
}
return result;
}
public String getting_ter(){
String result="";
String resultter=null;
String connstr2 = "My connection string 2";
try {
URL url = new URL(connstr2);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
InputStream ips = http.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = "";
//Line by Line reading the data from php
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
//String it together into one line
resultter = sb.toString();
result=resultter;
br.close();
ips.close();
http.disconnect();
} catch (MalformedURLException e) {
result = e.getMessage();
} catch (IOException e) {
result = e.getMessage();
}
return result;
}
答案 0 :(得分:0)
我看不到代码中的错误。我为您整理了一下,但是它应该在我更改之前和之后都可以使用。您没有描述它实际上出了什么问题。在调试中,您可能只是在连接上超时。
public String doInBackground(String... voids) {
String result = getting_kat() + "#KATEGORIA/TERMEK#" + getting_ter();
//Making sure it gets put into the safety string
safety_string = result;
return result;
}
public String getting_kat() {
String result = "";
String connstr = "My connection string";
try {
HttpURLConnection http = connect(connstr);
result = readFromPhp(http);
http.disconnect();
} catch (MalformedURLException e) {
result = e.getMessage();
} catch (IOException e) {
result = e.getMessage();
}
return result;
}
public String getting_ter() {
String result = "";
String connstr = "My connection string 2";
try {
HttpURLConnection http = connect(connstr);
result = readFromPhp(http);
http.disconnect();
} catch (MalformedURLException e) {
result = e.getMessage();
} catch (IOException e) {
result = e.getMessage();
}
return result;
}
private HttpURLConnection connect(String connstr) throws IOException {
URL url = new URL(connstr);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
return http;
}
private String readFromPhp(HttpURLConnection http) throws IOException {
// try-with-resources - so you don't have to call close()
try (
InputStream ips = http.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 8)
) {
StringBuilder sb = new StringBuilder();
String line;
//Line by Line reading the data from php
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
//String it together into one line
return sb.toString();
}
}