Android Httppost

RixPvl

Well-known member
30.11.2011
85
0
#1
Привет всем!
Стокнулся с проблемой по отправле запроса POST на сервер с Android приложения
Имеется код:
Код:
	public String sendPostReq()
{

String result = new String("");
try{
HttpClient client =new DefaultHttpClient();
HttpPost post = new HttpPost("http://android.xdev.kz/index.php");

// если используется прокси //
int portOfProxy = android.net.Proxy.getDefaultPort();
if( portOfProxy > 0 ){
HttpHost proxy = new HttpHost(android.net.Proxy.getDefaultHost(), portOfProxy );
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
/////////////////////////////
List pairs = new ArrayList();
pairs.add(new BasicNameValuePair("user", "19"));
post.setEntity(new UrlEncodedFormEntity(pairs));


// HttpResponse
HttpResponse response = client.execute(post);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"windows-1251"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + System.getProperty("line.separator"));
}
result = sb.toString();

} catch (org.apache.http.client.ClientProtocolException e) {
// TODO Auto-generated catch block
result = "ClientProtocolException: " + e.getMessage();
} catch (IOException e) {
// TODO Auto-generated catch block
result = "IOException: " + e.getMessage();

} catch (Exception e) {
// TODO Auto-generated catch block
result = "Exception: " + e.getMessage();
e.printStackTrace();
}
return result;
}
В манифесте указал привелегии на доступ к интернету но при вызове возвращает
Exception: null

<div class="sp-wrap"><div class="sp-head-wrap"><div class="sp-head folded clickable">Stack Trace</div></div><div class="sp-body"><div class="sp-content">
08-12 05:09:47.842: WARN/System.err(1418): android.os.NetworkOnMainThreadException
08-12 05:09:47.852: WARN/System.err(1418): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-12 05:09:47.852: WARN/System.err(1418): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-12 05:09:47.852: WARN/System.err(1418): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-12 05:09:47.852: WARN/System.err(1418): at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-12 05:09:47.861: WARN/System.err(1418): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(Default
ClientConnectionOperator.java:137)
08-12 05:09:47.861: WARN/System.err(1418): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-12 05:09:47.861: WARN/System.err(1418): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapt
er.java:119)
08-12 05:09:47.861: WARN/System.err(1418): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirecto
r.java:360)
08-12 05:09:47.872: WARN/System.err(1418): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:5
55)
08-12 05:09:47.872: WARN/System.err(1418): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:4
87)
08-12 05:09:47.882: WARN/System.err(1418): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:4
65)
08-12 05:09:47.882: WARN/System.err(1418): at com.example.Proj2.MyActivity.getCurrenciesRates(MyActivity.java:98)
08-12 05:09:47.882: WARN/System.err(1418): at com.example.Proj2.MyActivity.onClick(MyActivity.java:132)
08-12 05:09:47.893: WARN/System.err(1418): at android.view.View.performClick(View.java:4204)
08-12 05:09:47.902: WARN/System.err(1418): at android.view.View$PerformClick.run(View.java:17355)
08-12 05:09:47.902: WARN/System.err(1418): at android.os.Handler.handleCallback(Handler.java:725)
08-12 05:09:47.912: WARN/System.err(1418): at android.os.Handler.dispatchMessage(Handler.java:92)
08-12 05:09:47.912: WARN/System.err(1418): at android.os.Looper.loop(Looper.java:137)
08-12 05:09:47.912: WARN/System.err(1418): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-12 05:09:47.922: WARN/System.err(1418): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 05:09:47.932: WARN/System.err(1418): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 05:09:47.932: WARN/System.err(1418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-12 05:09:47.932: WARN/System.err(1418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-12 05:09:47.932: WARN/System.err(1418): at dalvik.system.NativeStart.main(Native Method)
 

RixPvl

Well-known member
30.11.2011
85
0
#2
Если кому интересно решил этот вопрос, для Android'a 4.0+ все подобные запросы нужно делать в отдельном потоке Thread, да бы не занимать основной поток который отвечает за UI, а лучше делать через AsyncTask.