我希望使用httpClient类在我的textview中使用html文件(web上的任何文件)。但它只显示我在android:text中设置的初始值。我无法获得所需的输出.....
帮助我......
这是代码......
## activity_mail.xml ##
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/inData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading..." />
MainActicity.java
package com.vivek.jsonpractice;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView stuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stuff= (TextView)findViewById(R.id.inData);
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getData();
stuff.setText(returned);
} catch (Exception e) {
e.printStackTrace();
}
}
}
GetMethodEx.java
package com.vivek.jsonpractice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class GetMethodEx {
public String getData() throws URISyntaxException, ClientProtocolException, IOException{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("https://www.google.co.in/");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while((l=in.readLine()) != null){
sb.append(l+nl);
}
in.close();
data = sb.toString();
return data;
}finally{
if(in != null){
try{
in.close();
return data;
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}