What is wrong in my android app with HttpPost? -
i'm making new android app, when send values httppost web file post.php, don't working. wrong ?
activity_activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".activityprincipal"> </relativelayout>
activityprincipal.java
package com.pixelayer.httppost.httppostandroid; import java.io.ioexception; import java.io.unsupportedencodingexception; import java.util.arraylist; import java.util.list; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import android.util.log; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; public class activityprincipal extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_activity_principal); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("www.mysite.com/post.php"); // replace // url list<namevaluepair> namevaluepair = new arraylist<namevaluepair>(2); namevaluepair.add(new basicnamevaluepair("nome", "test_user")); namevaluepair.add(new basicnamevaluepair("site", "testeandroid")); // encoding data try { httppost.setentity(new urlencodedformentity(namevaluepair)); } catch (unsupportedencodingexception e) { // log exception e.printstacktrace(); } // making request try { httpresponse response = httpclient.execute(httppost); // write response log log.d("http post response:", response.tostring()); } catch (clientprotocolexception e) { // log exception e.printstacktrace(); } catch (ioexception e) { // log exception e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_activity_principal, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pixelayer.httppost.httppostandroid" > <uses-permission android:name="android.permission.internet" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".activityprincipal" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
thank much
you doing http request in main thread, if logcat can see it's not allowed.
you should use asynctask or library (like volley or loopj).
to force android performe must change strictmode policy (insert code before http request)
strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy);
asynctask example
private class httptask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { string response = ""; ...performe request return response; } @override protected void onpostexecute(string result) { .../use response }
}
to call...
httptask task = new httptask(); task.execute(new string[] { "http://www.url.com" });
for loopj there doc http://loopj.com/android-async-http/ volley https://developer.android.com/training/volley/index.html
Comments
Post a Comment