2016-05-12 16 views
0

http post.iでアンドロイドからデータを渡したいのですが、送信ボタンをクリックしたときにウェブに表示されず、問題を解決できます。 ここに主要なアクティビティコードがあります。アンドロイドHTTP POST後の問題

public class HttpPostExample extends Activity { 

    TextView content; 
    EditText fname,email,login,pass; 
    String Name,Email,Login,Pass; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_http_post_example); 

     content = (TextView)findViewById(R.id.content); 
     fname =(EditText)findViewById(R.id.name); 
     email =(EditText)findViewById(R.id.email); 
     login =(EditText)findViewById(R.id.loginname); 
     pass =(EditText)findViewById(R.id.password); 


     Button saveme=(Button)findViewById(R.id.save); 
     saveme.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v) 
      { 
       try{ 

        GetText(); 
       } 
       catch(Exception ex) 
       { 
        content.setText("url exeption!");  
       } 
      } 
     }); 
    } 


    public void GetText() throws UnsupportedEncodingException 
    { 

     Name = fname.getText().toString(); 
     Email = email.getText().toString(); 
     Login = login.getText().toString(); 
     Pass = pass.getText().toString(); 



      String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(Name, "UTF-8"); 
      data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(Email, "UTF-8"); 
      data += "&" + URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(Login, "UTF-8"); 
      data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(Pass, "UTF-8"); 

      String text = ""; 
      BufferedReader reader=null; 
      // Send data 
     try 
     { 

      URL url = new URL("http://androidexample.com/localhost/web/Httppost.php"); 

      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(data); 
      wr.flush(); 
     // Get the response 


     reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 


      while((line = reader.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 
      text = sb.toString(); 
     } 
     catch(Exception ex) 
     { 

     } 
     finally 
     { 
      try 
      { 

       reader.close(); 
      } 
      catch(Exception ex) {} 
     } 

     content.setText(text); 

    } 

} 

ここではメインxmlコードです。

*<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
     android:stretchColumns="*" android:background="#ffffff"> 


    <TableRow android:background="#758AA7" android:layout_margin="2dip"> 
      <TextView android:id="@+id/output" 
      android:layout_height="wrap_content" 
      android:padding="1px" 
      android:text="@string/default_output" android:layout_width="wrap_content" /> 
      <TextView android:id="@+id/content" 
      android:layout_height="wrap_content" 
      android:padding="2px" 
      android:text="@string/default_content" android:layout_width="fill_parent" /> 
    </TableRow> 
    <TableRow android:background="#758AA7" android:layout_margin="2dip"> 

     <Button android:text="Save On Web" 
     android:id="@+id/save" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     /> 
     </TableRow> 

    <TableRow android:background="#ffffff" android:layout_margin="2dip"> 

     <TextView style="@style/CodeFont" 
     android:text="@string/Name" android:layout_width="30px" android:layout_gravity="left"/> 
     <EditText android:id="@+id/name" 
     android:layout_width="wrap_content" 
      android:gravity="left" android:layout_gravity="left" android:width="210px"/> 
    </TableRow> 
    <TableRow android:background="#ffffff" android:layout_margin="2dip"> 

     <TextView style="@style/CodeFont" 
     android:text="@string/Email" android:layout_gravity="left"/> 
     <EditText android:id="@+id/email" 
     android:layout_width="wrap_content" android:layout_gravity="left" android:width="210px"/> 
    </TableRow> 
    <TableRow android:background="#ffffff" android:layout_margin="2dip"> 

     <TextView style="@style/CodeFont" 
     android:text="@string/LoginName" android:layout_gravity="left"/> 
     <EditText android:id="@+id/loginname" 
     android:layout_width="wrap_content" android:layout_gravity="left" android:width="210px"/> 
    </TableRow> 
    <TableRow android:background="#ffffff" android:layout_margin="2dip"> 

     <TextView style="@style/CodeFont" 
     android:text="@string/Password" android:layout_gravity="left"/> 
     <EditText android:id="@+id/password" 
     android:layout_width="wrap_content" android:layout_gravity="left" android:width="210px"/> 
    </TableRow> 
</TableLayout>* 

それはウェブ上で表示されませんウェブ上の保存私は上をクリックすると、これはHttppost.php

<?php 

     $name = urldecode($_POST['name']); 
     $user = urldecode($_POST['user']); 
     $email = urldecode($_POST['email']); 
     $pass = urldecode($_POST['pass']); 

     print " ==== POST DATA ===== 
     Name : $name 
     Email : $email 
     User : $user 
     Pass : $pass"; 

?> 

私のPHPスクリプトです。

+0

ハニカムでnetworkonMAinThreadExceptionを取得 あなたが使用asynctask –

+0

がPHPでエラーをエコーし​​てください理由であるメインスレッド上でそれをやっている – manolodewiner

+0

申し訳ありません私はあなたが私に適切な解決策を与えてください言うことを理解していません。 –

答えて

0

httpurlconnectionコードをバックグラウンドスレッドで実行して問題を解決してください。あなたはMAinthreadで実行する場合は、以降

+0

httpurlconnection codeどのように変更するのですか?私は新しい助けてください –

+0

私はアンドロイドからウェブにローカルホスト上のデータを送信することができます –

+0

asynaskを使用し、そのメソッドをasynctaskのdoinbackgroundで呼び出します –