我的代码如下所示:
public class YActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
Log.d(TAG, "onCreate: page response" + getResponseFromUrl("www.iammjet.in"));
} catch (IOException e) {
e.printStackTrace();
}
}
public String getResponseFromUrl(String url) throws IOException {
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet(URL); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
String resString = sb.toString();
is.close();
return resString;
}
}
现在发生的事情是它给了我Target host must not be null, or set in parameters. scheme=null, host=null, path=www.iammjet.in
错误,我应该插入什么才能使它工作?
我正在使用compileSdkVersion 24.为了支持HttpClient,我在build.gradle中添加了如下内容:
android {
compileSdkVersion 24
buildToolsVersion "25.0.1"
useLibrary 'org.apache.http.legacy'
}
我的错误堆栈跟踪如下所示:
java.lang.RuntimeException: Unable to start activity ComponentInfo{demand.inn.com.xyz.staging/demand.inn.com.xyz.activity.YActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
at demand.inn.com.quflip.activity.YActivity.getResponseFromUrl(YActivity.java:328)
at demand.inn.com.quflip.activity.QuFlipActivity.onCreate(QuFlipActivity.java:94)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
--------- beginning of crash
答案 0 :(得分:2)
您的代码存在三个主要问题:
格式化您的网址
您的浏览器对您而言。 http
不是网址。它只是一个主机名。要成为一个URL,你必须有一个方案,主机和路径(正如错误告诉你的那样。
www.iammjet.in
有效。它具有方案/
,主机{{1}}和路径{{1}}。因为你只给了主机名,所以它误解了它只是路径,并说主机名为空。
答案 1 :(得分:0)
传递“http://www.iammjet.in”而不是“www.iammjet.in”