Android Httprequest 不工作

Android Httprequest is not working

我是 android 的新人,在这里我想请求一个网页并获得响应。我已经从教程中编写了这段代码,尽管我写的完全一样,并且我已经在清单文件中授予了 Internet 访问权限,但它不起作用或没有 returning 任何东西。这是调用命名 MainActivity.java 的主要 activity 包裹 com.example.limon.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv = (TextView) findViewById(R.id.txt1);

    WebResult web = new WebResult();
    String result = "";
    try {
        result = web.getResult();
    } catch (Exception e) {
        e.printStackTrace();
    }
    tv.setText(result);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

} >

这是另一个 class return 响应

package com.example.limon.myapplication;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;

/**
 * Created by Limon on 3/11/2015.
 */
public class WebResult {

public String getResult() throws  Exception{
    BufferedReader in = null;
    String data = null;

    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://www.mybringback.com");
        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();
            }
        }
    }
}

}

您是否将互联网权限添加到 android 清单中? <uses-permission android:name="android.permission.INTERNET" />

不能在主线程中使用所有 http 请求。 阅读:AsyncTask

转到您的项目并打开 AndroidManifest.xml 并包含类似

的代码
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

这将允许与 Internet 交互

尝试将此属性添加到您的 AndroidManifest.xml 以允许所有 HTTP 请求:

android:usesCleartextTraffic="true"