我从HTTPPOST获得响应,从中获取一个子字符串,即URL,然后在其上执行HTTPGET,我无法获得任何响应。有关问题的任何意见?
String pkmspogout1 = result.substring(result.indexOf(",")+1,result.length());
Log.i("Lpkmspogout1 ", pkmspogout1);
HttpClient httpclient4 = new DefaultHttpClient();
HttpResponse logoutresponse ;
try {
HttpGet httpget = new HttpGet(pkmspogout1);
logoutresponse = httpclient4.execute(httpget);
BufferedReader in2 = new BufferedReader(new InputStreamReader(logoutresponse.getEntity().getContent()));
StringBuffer sb2 = new StringBuffer("");
String line2 = "";
String NL2 = System.getProperty("line.separator");
while ((line2 = in2.readLine()) != null) {
sb2.append(line + NL2);
}
in2.close();
String pkmslogout5 = sb2.toString();
Log.i("logoutresponse from pkms", pkmslogout5+"");
} catch (Exception e) {}
答案 0 :(得分:3)
pkmspogout1 是否是正确的网址? 此外,您应该确保执行以下操作以查看任何错误:
catch (Exception e) {e.printStackTrace();}
尝试按如下方式对网址进行编码:
String[] url = pkmspogout1.split("com"); //Or whatever is the last part of the main url.
//The letter "." means any character btw as it is a regular expression.
String finalUrl = url[0];
for(int i=0; i<url.length; i++){
finalUrl += URLEncoder.encode(url[i],"UTF-8");
}
说实话有点粗糙......为什么在服务器端形成时会更好地编码呢。
你可以尝试:
pkmspogout1 = pkmspogout1.replace("http://","");
pkmspogout1 = URLEncoder.encode(pkmspogout1);
pkmspogout1 = "http://"+pkmspogout1;
查看是否只是“http://”破坏了您的网址。我仍然认为这是最重要的问题,因为你告诉我硬编码它的工作正常。
答案 1 :(得分:0)
终于工作了,Phew!我试过(pkmspogout1.trim())并且它有效! (URL之前和之后的空格是问题)。