I want to use httpclient in my android app. I know this library is deprecated a long time ago but I want to use it for some reason. When I write some code to fetch JSON data from an URL the emulator fetching me this: image
this is my code :
public class FirstExampleActivity extends AppCompatActivity {
TextView tv ;
public static final String URL = "http://androidtestapp.gigfa.com/mysite/guitarist.json";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_example);
tv = findViewById(R.id.tv);
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuItem item = menu.add("GET");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Thread thread = new Thread(new Runnable() {
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String content = msg.getData().getString("content");
tv.setText(content);
}
};
@Override
public void run() {
String content = getData();
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("content",content);
message.setData(bundle);
handler.sendMessage(message);
}
});
thread.start();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private String getData() {
HttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(URL);
try{
HttpResponse response = client.execute(method);
InputStream stream = response.getEntity().getContent();
String content = Utils.inputStreamToString(stream);
return content ;
}
catch (Exception ex){
ex.printStackTrace();
}
return null ;
}
}
I believe this answer can help you. The problem is that some sites at first return some javascript, for example for validation purposes, but httpclient doesn't support javascript.