本文介绍了Android的HTTP GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Android开发的新手。我试图发送一个GET请求的URL。我写了下面的code。
I'm a newbie at android development. I'm trying to send a GET request to an URL. I wrote the below code.
public void searchProducts(View v) { //String txtSearchTerm = ((EditText)findViewById(R.id.txtsearch)).getText().toString(); //String termCleaned = txtSearchTerm.replace(' ', '+').toString(); AlertDialog alertMessage = new AlertDialog.Builder(this).create(); alertMessage.setTitle("Loading"); alertMessage.setMessage(GET("webkarinca/sample.json")); alertMessage.show(); } public static String GET(String url){ InputStream inputStream = null; String result = ""; try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse httpResponse = httpclient.execute(new HttpGet(url)); inputStream = httpResponse.getEntity().getContent(); if(inputStream != null) { result = convertInputStreamToString(inputStream); } else { result = "Did not work!"; } } catch (Exception e) { Log.d("InputStream", e.getLocalizedMessage()); } return result; } private static String convertInputStreamToString(InputStream inputStream) throws IOException{ BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); String line = ""; String result = ""; while((line = bufferedReader.readLine()) != null) result += line; inputStream.close(); return result; }我已经把类的进口头。他们在那里
I already put imports head of the class. There they are
import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONObject;它不工作,并在问题部分它显示为警告
It doesn't work and at the Problems section it shows as a warning
该型HTTPGET是德precated该类型的Htt presponse是德precated
The type HttpGet is deprecated The type HttpResponse is deprecated
推荐答案试试这个code。这为我工作。
Try this code. This worked for me.
import java.io.IOException; import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; public class ServerTest extends Activity { private String TAG = "test"; private String url = "webkarinca/sample.json"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new Download().execute(); } public class Download extends AsyncTask<Void, Void, String>{ @Override protected String doInBackground(Void... params) { String out = null; try { DefaultHttpClient httpClient = new DefaultHttpClient(); final HttpParams httpParameters = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 15000); HttpConnectionParams.setSoTimeout(httpParameters, 15000); HttpGet httpPost = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); out = EntityUtils.toString(httpEntity, HTTP.UTF_8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return out; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.e(TAG, result); } } }另外,还要确保你已经加入这个表现,
Also make sure you have added this to manifest,
<uses-permission android:name="android.permission.INTERNET" />,并且确保您连接到互联网。
and also make sure you are connected to the internet.
更多推荐
Android的HTTP GET请求
发布评论