2016-07-22 6 views
0

から文字列を受け取る:アンドロイド - PHPは、私はPHPスクリプト次ているアンドロイド

<?php 
$parameter=$_POST['uploaded_text']; 

$data = $parameter.PHP_EOL; 
$fp = fopen('uploads/test.txt', 'a'); 
fwrite($fp, $data); 

?> 

がどのように私は$parametersにAndroidデバイスから文字列値を与えることができますか? 私はHttpURLConnectionでチュートリアルを探していましたが、私はまだ使えない 'HttpClient'しか見つけませんでした。 誰かが私が単にそれをやり遂げる方法を教えてもらえますか? ありがとうございます!

+0

http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post私はこれがあなたの質問 –

+0

にも答えると思いますので、http://stackoverflow.com/questionsの複製/ 9767952/how-to-add-parameters-to-httpurlconnection-using-post –

答えて

1

私の提案は、StringRequestsを見てみることです。ここでは、データベースを更新PHPファイルに物事を送信する方法の一例である、または何を行うことができます。

LoginRequest.java:

package com.example.your_app.database_requests; 

import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.toolbox.StringRequest; 

import java.util.HashMap; 
import java.util.Map; 

public class LoginRequest extends StringRequest { 

    private static final String LOGIN_REQUEST_URL = "http://example.com/Login.php"; 
    private Map<String, String> params; 

    public LoginRequest(String username, String password, Response.Listener<String> listener) { 
     super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null); 
     params = new HashMap<>(); 
     params.put("username", username); 
     params.put("password", password); 
    } 

    @Override 
    public Map<String, String> getParams() { 
     return params; 
    } 

} 

StringRequestを送信し、リターンを得るために、値:

Response.Listener<String> listener = new Response.Listener<String>() { 

       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonResponse = new JSONObject(response); 

         boolean success = jsonResponse.getBoolean("success"); 
         // Anything else that your php file returns will go here (like the boolean success) 

         if (!success) { 
          Log.e(TAG, "Could not login."); 
         } else { 
          Log.e(TAG, "Logged in."); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 
      LoginRequest loginRequest = new LoginRequest(username, password, listener); 
      RequestQueue requestQueue = Volley.newRequestQueue(context); 
      requestQueue.add(loginRequest); 

Login.php:

<?php 

    $password = $_POST["password"]; 
    $username = $_POST["username"]; 

    $con = mysqli_connect("website.com", "dbusername", "dbpassword", "dbtable"); 

    $response = array(); 
    $response["success"] = false; 

    /* Do something */ 

    $response["success"] = true; 

    echo json_encode($response); 
?> 
+0

本当に助けてくれてありがとう! – killertoge

+0

問題ありません!それがあなたを助けてくれてうれしいです。 – TheAnonymous010

+0

私はPHPから文字列を受け取って反対にしたい場合は、POSTからGETに変更する必要がありますか? params.put( "thestring"、savetohere)?仕事? – killertoge

1

多くのチュートリアルは、原料のこの種のためにオンラインがあります。

try { 

     String url = "http://yoursiteurlhere.com" ; 

     URL myURL = new URL(url); 

     HttpURLConnection httpURLConnection = (HttpURLConnection) myURL.openConnection(); 

     httpURLConnection.setRequestMethod("POST"); 

     String body = params[0]; 

     httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
     httpURLConnection.setRequestProperty("Accept", "application/json"); 
     httpURLConnection.setDoOutput(true); 
     httpURLConnection.connect(); 

     DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
     wr.writeBytes(body); 
     wr.flush(); 
     wr.close(); 

     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); 

     String inputLine; 
     stringBuffer = new StringBuffer(); 

     while((inputLine = bufferedReader.readLine()) != null){ 
      stringBuffer.append(inputLine); 
     } 

     bufferedReader.close(); 

     return stringBuffer.toString(); 
    }catch (MalformedURLException e){ 
     e.printStackTrace(); 
    }catch (IOException e){ 
     e.printStackTrace(); 
    } 

バックグラウンドスレッドでこれを行うためにかかわらず、忘れないでください!

exampleについてはこちらを参照してください。

関連する問題