我收到了这段代码,为什么它没有向“toBeSplit”字符串返回任何值? 我这样称呼方法。
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getData();
}
}).start();
这是实际的方法,我尝试了1000种方法,它只是工作..
private void getData() {
StringBuilder builder = new StringBuilder();
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
HttpClient client = new DefaultHttpClient(httpParams);
String yourUrl = "http://www.ratemyplays.com/form.php";
HttpGet httpGet = new HttpGet(yourUrl);
{
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
toBeSplit = builder.toString();
split2 = toBeSplit.split(",");
}
答案 0 :(得分:1)
使用代码:
StringBuilder builder = new StringBuilder();
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
HttpClient client = new DefaultHttpClient(httpParams);
String yourUrl = "http://www.ratemyplays.com/form.php";
HttpGet httpGet = new HttpGet(yourUrl);
{
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
String tempstr = builder.toString();
String str[] = tempstr.split(",");
答案 1 :(得分:0)
尝试拆分如下:
try {
response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream in = response.getEntity().getContent();
StringBuilder sb = new StringBuilder();
String line = "";
BufferedReader bf = new BufferedReader(
new InputStreamReader(in));
while ((line = bf.readLine()) != null) {
sb.append(line);
}
m_response = sb.toString();
StringBuilder split = new StringBuilder();
split =m_response.split(",");
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}