2014-03-29 103 views
3

好,英文不多,但尽量清楚。Android HTTP-POST

我在java中很新手。我几天前开始。我正在尝试开发一个arhico php的网站阅读。但它不起作用。总是让我回味

我认为我们应该用Thread做点什么,但是我不知道如何。

我正在使用Android工作室。

我公开了我拥有的数据。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.tuto1.app" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:maxSdkVersion="15"/> 
    <uses-permission android:name="android.permission.INTERNETr"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.tuto1.app.MainActivity" 
      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> 

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="com.example.tuto1.app.MainActivity"> 

<TextView 
    android:text="@string/hello_world" 
    android:layout_width="fill_parent" 
    android:layout_height="200dp" 
    android:background="#044234" 
    android:textColor="#ffff" 
    android:textSize="30sp" 
    android:id="@+id/text1" /> 

MainActivity.java

package com.example.tuto1.app; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     httpHandler handler = new httpHandler(); 
     String txt = handler.post("http://demo.com/demo.php"); 

     TextView t = (TextView)findViewById(R.id.text1); 
     t.setText(txt); 



    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

httpHander.java(类)

package com.example.tuto1.app; 

    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.apache.http.util.EntityUtils; 

public class httpHandler { 

    public String post(String posturl){ 

     try { 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(posturl); 

      HttpResponse resp = httpclient.execute(httppost); 
      HttpEntity ent = resp.getEntity(); 

      String text = EntityUtils.toString(ent); 

      return text; 

     } 
     catch (Exception e) { return "mi error"; } 

    } 

} 

更新该溶液是这样的。

import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     class TheTask extends AsyncTask<String,Void,String> 
     { 

      @Override 
      protected String doInBackground(String... arg0) { 
       String text =null; 
       try { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost(arg0[0]); 
        HttpResponse resp = httpclient.execute(httppost); 
        HttpEntity ent = resp.getEntity(); 
        text = EntityUtils.toString(ent); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 

       return text; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       super.onPostExecute(result); 

       TextView t = (TextView)findViewById(R.id.text1); 
       t.setText(result); 
      } 

     } 

     new TheTask().execute("http://demo.com/php.php"); 


    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

回答

1

您需要使用ThreadAysnctaskVolley库。您不能在主UI线程上运行网络操作。

HttpResponse resp = httpclient.execute(httppost); // must be in a thread. 

你可以创建自己的Java线程,但的AsyncTask使得它更容易。你会发现信息和示例@

http://developer.android.com/reference/android/os/AsyncTask.html

例子:

public class MainActivity extends Activity { 
     TextView t; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); // activity main is the activity that you are currently working with 
      t = (TextView)findViewById(R.id.text1); // this is the text view where you want the text response to appear. 
      new TheTask().execute("http://demo.com/demo.php"); 
     } 

     class TheTask extends AsyncTask<String,Void,String> 
     { 

     @Override 
     protected String doInBackground(String... arg0) { 
      String text =null; 
      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(arg0[0]); 
       HttpResponse resp = httpclient.execute(httppost); 
       HttpEntity ent = resp.getEntity(); 
       text = EntityUtils.toString(ent); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

      return text; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      t.setText(result); 
     } 

     } 
} 
+0

感谢您的帮助。我正在尝试做,但此刻我返回空 – user3476200

+0

@ user3476200它不是我建议的代码的问题。记录'text'的值。检查你是否有任何东西。 asynctask没有错。顺便说一下,我回到空的意思是什么? – Raghunandan

+0

引起:libcore.io.ErrnoException:getaddrinfo失败:EACCES(权限被拒绝) 对不起,我不明白英文的esque。 – user3476200