2016-05-12 5 views
1

アンドロイドからウェブへのポストデータアンドロイドからウェブにデータを送るアンドロイドからデータを送信するアンドロイドアプリはデータパーフェクトを送信するが、私はアンドロイドからデータを受け取るためにPHPでコーディングする方法を知らない。 私はphpスクリプトのためにxamppサーバを使用しています。これはmtアプリコードです。アンドロイドHTTPポストPHPスクリプトの問題

public class Main extends Activity implements OnClickListener{ 

    private EditText value; 
    private Button btn; 
    private ProgressBar pb; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home_layout); 
     value=(EditText)findViewById(R.id.editText1); 
     btn=(Button)findViewById(R.id.button1); 
     pb=(ProgressBar)findViewById(R.id.progressBar1); 
     pb.setVisibility(View.GONE); 
     btn.setOnClickListener(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.home, menu); 
     return true; 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
      if(value.getText().toString().length()<1){ 

       // out of range 
       Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show(); 
      }else{ 
       pb.setVisibility(View.VISIBLE); 
       new MyAsyncTask().execute(value.getText().toString());  
      } 


    } 

    private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

     @Override 
     protected Double doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      postData(params[0]); 
      return null; 
     } 

     protected void onPostExecute(Double result){ 
      pb.setVisibility(View.GONE); 
      Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show(); 
     } 
     protected void onProgressUpdate(Integer... progress){ 
      pb.setProgress(progress[0]); 
     } 

     public void postData(String valueIWantToSend) { 
      // Create a new HttpClient and Post Header 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://somewebsite.com/receiver.php"); 

      try { 
       // Add your data 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       // Execute HTTP Post Request 
       HttpResponse response = httpclient.execute(httppost); 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
      } 
     } 

    } 
} 

この私の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" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:text="Enter Something Below:" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="30dp" 
     android:ems="10" 
     android:hint="" 
     > 

     <requestFocus /> 
    </EditText> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="24dp" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_alignRight="@+id/editText1" 
     android:layout_below="@+id/progressBar1" 
     android:layout_marginTop="24dp" 
     android:text="Submit" /> 

</RelativeLayout> 

これはPHPスクリプトコードreciver.php

<?php 
// receive data from app's http request 
$data=$_POST["myHttpData"]; 
// write data from my android app to a text file 
file_put_contents('myTextFile.txt',$data); 
?> 

である私は、適切なコードでPHPスクリプトを作ることができる方法

それショーを教えてくださいPHPのこのような私のエラー。 enter image description here

答えて

0

はtest.phpをという名前のファイルを作成し、そのファイルにPHPコードをコピーして、手順

  1. に従ってください。
  2. そのファイルをxampサーバーのルートフォルダ(WWWディレクトリ内)にコピーします。
  3. モバイルとコンピュータが同じWiFiルーターに接続されていることを確認してください。
  4. コピーされたIPアドレスがサーバーのIPアドレスになります。検索ポート番号を行う必要があるかもしれません。ポート番号/ test.phpを 例http://192.168.1.1:8080/test.php
  5. があなたのXAMPサーバーを実行します。あなたはURLでこれ http://IP adreasのようないくつかのことを終わる上記の4ステップの後
  6. あなたのAndroidのhttp要求にこのURLを設定してください。
  7. Googleでエラーを探します。
+0

私はphpファイルを開いたときにエラーが出るので、私はPHPスクリプトエラーのスクリーンショットを投稿してください。 –

+0

私の質問をチェックしてください私はそれを編集します –

+0

明らかにそれはなります。ブラウザでURLを打つと、ポストパラメータは含まれません。いくつかの基本的なPHPのコードを確認してください – Sush