Android / OkHttp获取源代码

时间:2015-07-11 03:09:26

标签: java android http okhttp

我想获取此页面的源代码“http://wankil.fr/dons”,并将其打印在TextView中,但没有打印出来我不知道为什么,需要一些帮助。

在这段代码中,我想得到捐赠者的名字,以及每次捐赠的评论。这些信息在代码源中,但我的TextView上没有打印任何内容仅用于测试,我不明白为什么......

public class Main extends ActionBarActivity {
private ListView                        vue;
private Vector<Vector<String>>          listDons = new Vector<Vector<String>>();
private List<HashMap<String, String>>   listHash = new ArrayList<HashMap<String, String>>();
private URL                             url;
private OkHttpClient                    client = new OkHttpClient();

public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vue = (ListView) findViewById(R.id.listView);
        this.createNewDonation("Raphael T.", "Vous etes trop fort");
        Toast.makeText(this, "MyString", Toast.LENGTH_SHORT).show();
        ListAdapter adapter = new SimpleAdapter(this,
                listHash,
                android.R.layout.simple_list_item_2,
                new String[]{"text1", "text2"},
                new int[]{android.R.id.text1, android.R.id.text2});
        vue.setAdapter(adapter);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

public String run(String url) throws IOException {
    Request request = new Request.Builder()
            .url(url)
            .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
}

public boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    Log.d("isOnline", "I'm in");
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

public void printText(View v) {
    try {
        Log.d("Here", "I'm in");
        if (this.isOnline() == true) {
            Log.d("Status:", "I'm online");
            String str;
            str = this.run("http://wankil.fr/dons");
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText(str);
            //this.createNewDonation(str.substring(0, 10), str.substring(10, 20));
        } else
            Log.d("Status:", "I'm not online");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void     createNewDonation(String text1, String text2){
    try {
        StringBuilder newDons = new StringBuilder();
        Vector<String> tmp = new Vector<String>();
        HashMap<String, String> element = new HashMap<>();

        tmp.add(text1);
        tmp.add(text2);
        listDons.add(tmp);
        element.put("text1", listDons.get(0).get(0));
        element.put("text2", listDons.get(0).get(1));
        listHash.add(element);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

}

错误:

07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ android.os.NetworkOnMainThreadException
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
07-11 15:49:48.696    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.Call.getResponse(Call.java:273)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at com.squareup.okhttp.Call.execute(Call.java:81)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at com.dot.wankilapp.Main.run(Main.java:78)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at com.dot.wankilapp.Main.printText(Main.java:96)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at android.view.View$1.onClick(View.java:4015)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at android.view.View.performClick(View.java:4780)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at android.view.View$PerformClick.run(View.java:19866)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-11 15:49:48.697    8109-8109/com.dot.wankilapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

0 个答案:

没有答案