2016-12-21 3 views
-1

verwende ich Volley Bibliothek in Android und erstellen Sie eine JsonObjectRequest wie folgt aus:Senden und Empfangen JsonRequest von Android zu Python

String JsonUrl = "192.168.1.3/json.py";  
JsonObjectRequest jsObjRequest = new JsonObjectRequest 
       (Request.Method.GET, JsonUrl, null, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         mTextView.setText("Json Response: " + response.toString()); 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         mTextView.setText("Error in recieving JSON Response"); 
        } 
       }); 
     // Access the RequestQueue through your singleton class. 
     MySingleton.getInstance(this).addToRequestQueue(jsObjRequest); 

und das ist mein Python-Code (json.py):

import json 

pythonDictionary = {'name':'Bob', 'age':44, 'isEmployed':True} 
dictionaryToJson = json.dumps(pythonDictionary) 
print(dictionaryToJson) 

Ich bin mir ziemlich sicher, dass mein Python-Code nicht korrekt ist. Mein Problem ist, dass, wie kann ich einen Code in Python schreiben senden & erhalten JsonObjects zu Android mit Get oder POST-Methode? Vielen Dank.

+0

Neben diesem Punkt. 'localhost' funktioniert nicht auf Ihrem Android-Gerät. Dies wurde immer wieder beantwortet. http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device –

+0

Ich habe Web-Server läuft – Matarata

+0

Ich habe meinen Laptop ip. Ich bearbeite meine Frage – Matarata

Antwort

1

ich lösen dieses Problem mit dieser Codes:

Android Code:

JsonObjectRequest jsObjRequest = new JsonObjectRequest 
       (Request.Method.GET, JsonUrl, null, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         try { 
          JSONObject obj = new JSONObject(response.toString()); 
          String name = obj.getString("name"); 
          String age = obj.getString("age"); 
          data += "Name: " + name + "\nAge : " + age; 
          mTextView.setText(data); 
         } 
         catch (JSONException e) { 
          mTextView.setText(e.toString()); 
         } 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         mTextView.setText("Error in recieving JSON Response:\n" + error.toString()); 
        } 
       }){ 
      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String,String> params = new HashMap<String, String>(); 
       params.put("Content-Type","application/x-www-form-urlencoded"); 
       return params; 
      } 
     }; 
     // Access the RequestQueue through your singleton class. 
     MySingleton.getInstance(this).addToRequestQueue(jsObjRequest); 

Python-Code:

from flask import Flask 
import json 

app = Flask(__name__) 

@app.route('/', methods=['GET']) 
def echo_msg(): 
    pythonDictionary = {'name': 'Bob', 'age': 44, 'isEmployed': True} 
    dictionaryToJson = json.dumps(pythonDictionary) 
    return dictionaryToJson 

if __name__ == '__main__': 
    app.run(host='0.0.0.0') 

So können Sie Flask Web-Server für Python ausführen und senden Sie Ihre JsonObject (mit GET-Methode) so.

Verwandte Themen