2017-03-25 2 views

Antwort

0

Sie sollten etwas tun:

public void postData() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
     nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi")); 
     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 
    } 
} 
+0

HttpPost httppost = neuer HttpPost ("http://www.yoursite.com/script.php"); . Der Link ist .net basiert nicht php. Wie wird es funktionieren? soll ich sowas schreiben? [link] http://example.com.in/ai/abc?var.aspx [link] und nameValuePairs.add (new BasicNameValuePair ("var", "12345")); – Ras

+0

http://www.yoursite.com/script.php ist ein Beispiel, Sie können Ihre eigene URL verwenden. –

+0

Da es .NET ist So soll ich schreiben? ** new HttpPost ("example.com.in/ai/abc?var.aspx"); ** und nameValuePairs.add (new BasicNameValuePair ("var", "12345")); – Ras

-1

Sie Android Volley-Bibliothek verwenden können, um Ihre Daten zu Ihrer PHP-Post script.Here ist das Beispiel von Volley Anfrage

//Declare the URL you want to send 
public static String YOUR_URL = "http://example.com.in/ai/abc?var="; 

private void postDataToServer() { 
    //volley request 

                //here you can choose your method,GET/POST,in this case is Post 
    JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.POST, 
      YOUR_URL , null, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 
      //if send data success do something here 
     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      //if having error to make request to server do something here 
     } 
    }) { 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      //send your parameter here 

      Map<String, String> parameters = new HashMap<>(); 
      parameters.put("yourVariable", "var"); 


      return parameters; 
     } 

     //adding header to authenticate 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 

      Map<String,String> headers = new HashMap<>(); 
      headers.put("Content-Type", "application/json"); 
      return headers; 
     } 
    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(jsonReq); 

} 

Sie Schauen Sie sich this tutorial an, um zu sehen, wie Sie Daten von Android veröffentlichen und die Daten in Ihrem PHP-Skript erhalten.

+0

ist es ein .NET-Skript. Funktioniert das? – Ras

+0

bro diese Java.Android Volley-Bibliothek – ken

+0

Ich bekomme Sie einen hilfreichen Link, erreichen Sie über diese erste https://developer.android.com/training/volley/index.html – ken

Verwandte Themen