亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Android HttpURLConnection進行Http獲取

使用Android HttpURLConnection進行Http獲取

慕村9548890 2019-10-09 16:57:50
我是Java和Android開發的新手,嘗試創建一個簡單的應用程序,該應用程序應與Web服務器聯系并使用http get將一些數據添加到數據庫中。當我在計算機上使用網絡瀏覽器撥打電話時,它工作正常。但是,當我進行在Android模擬器中運行應用程序的通話時,不會添加任何數據。我已將Internet權限添加到該應用的清單中。Logcat不報告任何問題。誰能幫助我找出問題所在?這是源代碼:package com.example.httptest;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;public class HttpTestActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView tv = new TextView(this);        setContentView(tv);        try {            URL url = new URL("http://www.mysite.se/index.asp?data=99");            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();            urlConnection.disconnect();            tv.setText("Hello!");        }        catch (MalformedURLException ex) {            Log.e("httptest",Log.getStackTraceString(ex));         }        catch (IOException ex) {            Log.e("httptest",Log.getStackTraceString(ex));        }       }        }
查看完整描述

3 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

這是完整的AsyncTask課程


public class GetMethodDemo extends AsyncTask<String , Void ,String> {

    String server_response;


    @Override

    protected String doInBackground(String... strings) {


        URL url;

        HttpURLConnection urlConnection = null;


        try {

            url = new URL(strings[0]);

            urlConnection = (HttpURLConnection) url.openConnection();


            int responseCode = urlConnection.getResponseCode();


            if(responseCode == HttpURLConnection.HTTP_OK){

                server_response = readStream(urlConnection.getInputStream());

                Log.v("CatalogClient", server_response);

            }


        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }


        return null;

    }


    @Override

    protected void onPostExecute(String s) {

        super.onPostExecute(s);


        Log.e("Response", "" + server_response);



    }

}


// Converting InputStream to String


private String readStream(InputStream in) {

        BufferedReader reader = null;

        StringBuffer response = new StringBuffer();

        try {

            reader = new BufferedReader(new InputStreamReader(in));

            String line = "";

            while ((line = reader.readLine()) != null) {

                response.append(line);

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            if (reader != null) {

                try {

                    reader.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

        return response.toString();

    }

召喚這AsyncTask堂課


new GetMethodDemo().execute("your web-service url");


查看完整回答
反對 回復 2019-10-09
  • 3 回答
  • 0 關注
  • 554 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號