Issue
The content that is present in the URL
is
[{"dspName":"openrtb","status":[{"type":"BIDREQ","state":"UP","avgTime":37},{"type":"VIEWTRACK","state":"UP","avgTime":10}]}]
but when I am reading the URL from my Android application, the output is
<html><head> <title>MSG: Please wait while you are redirected</title> <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1"> <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> <META HTTP-EQUIV="EXPIRES"CONTENT="0"> <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> <script language="JavaScript" type="text/JavaScript"> function onLoad() { window.location="http://182.16.0.1/userportal/?callerIP=182.16.11.24&requestURL=http%3A%2F%2Fsspapi-dev.samsungrs.com%2Fhealth"; } </script></head><body onLoad="setTimeout('onLoad()', 1000);"><br><center>Please wait while you are redirected to the login page<br></center></body></html>
Please tell me what I am doing wrong.
Here is my code.
MainActivity.java
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity implements AsyncResponse {
TextView t1;
final String website = "http://sspapi-dev.samsungrs.com/health";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = findViewById(R.id.textview1);
Viewdata v1 = new Viewdata();
v1.delegate = this;
v1.execute(website);
}
@Override
public void processFinish(String output) {
t1.setText(output);
}
}
class Viewdata extends Urltask{
public AsyncResponse delegate = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String dataModels) {
super.onPostExecute(dataModels);
delegate.processFinish(dataModels);
}
}
JSONhelper.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class JSONhelper{
HttpURLConnection connection;
String data;
public String getdatafromurl(String url){
String result = new String();
try{
URL url1 = new URL(url);
connection = (HttpURLConnection) url1.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null){
buffer.append(line);
}
result = buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
AsyncResponse.java
public interface AsyncResponse {
void processFinish(String output);
}
Urltask.java
import android.os.AsyncTask;
abstract class Urltask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
JSONhelper jsonhelper = new JSONhelper();
String data = jsonhelper.getdatafromurl(params[0]);
return data;
}
}
By the way, it seems that the DefaultHttpClient package and its corresponding functions have been deprecated in API 22 (Android Lollipop) and removed from API 23 (Android Marshmallow). Making use of AsyncTask seems to be one of the alternative methods to it. But are there any other alternative methods that are available to it, that are closely related to the way of old DefaultHttpClient package and its corresponding functions themselves?
Solution
package com.example.sample;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadContent();
}
public static String GET(OkHttpClient client, String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
@SuppressLint("StaticFieldLeak")
private void loadContent() {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();
String response = "";
try {
response = GET(client, "http://sspapi-dev.samsungrs.com/health");
//Parse the response string here
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e("Response", s);
}
}.execute();
}
}
E/Response: [{"dspName":"openrtb","status":[{"type":"BIDREQ","state":"UP","avgTime":37},{"type":"VIEWTRACK","state":"UP","avgTime":10}]}]
add this depandancies in gradle
compile 'com.squareup.okhttp3:okhttp:3.12.0'
Answered By - Tarun Umath
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.