URL中显示的内容
是
[{"dspName":"openrtb","status":[{"type":"BIDREQ","state":"UP","avgTime":37},{"type":"VIEWTRACK","state":"UP","avgTime":10}]}]
但是当我从Android应用程序中读取URL时,输出为
<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>
请告诉我我在做错什么。
这是我的代码。
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);
}
Ultask.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;
}
}
顺便说一句,似乎在API 22(Android Lollipop)中已弃用DefaultHttpClient软件包及其相应功能,并已从API 23(Android Marshmallow)中删除了它。利用AsyncTask似乎是它的替代方法之一。但是,还有其他可用的替代方法,它们与旧的DefaultHttpClient软件包及其相应功能本身的方式密切相关吗?
答案 0 :(得分:1)
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 /响应: [{“ dspName”:“ openrtb”,“状态”:[{“ type”:“ BIDREQ”,“ state”:“ UP”,“ avgTime”:37},{“ type”:“ VIEWTRACK”,“状态“:” UP“,” avgTime“:10}]}]
在gradle中添加此单位偏差
compile 'com.squareup.okhttp3:okhttp:3.12.0'