Multipartentity und Json kombinieren

T

t18935

Neues Mitglied
0
Hallo,

da es bei meinem anderen Thema so gut geklappt hat versuche ich es gleich nochmal :)

Bisher kommuniziere ich mit meiner App mit meinem Server (PHP) über JSON.
Also:

Code:
                    JSONObject json = new JSONObject();
                    try {
                        json.put("UserName", "Max");
                        json.put("FullName", "Mustermann");

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                    HttpParams httpParams = new BasicHttpParams();
                    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
                    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
                    HttpClient client = new DefaultHttpClient(httpParams);

                    HttpPost request = new HttpPost(b_urlsenden);
                    try {
                        request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
                        request.setHeader("json", json.toString());
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                    
                    
                    HttpResponse response;
                    try {
                        response = client.execute(request);

                        }
                        
                        
            
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
Nun lade ich ein Foto hoch mit dem MultipartEntity:

Code:
HttpClient httpClient = new DefaultHttpClient();                        
        HttpPost httppost = new HttpPost(urlString);
        File file = new File(imageend);
        HttpResponse response = null;                        
        
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "image/jpeg");
        mpEntity.addPart("userfile", cbFile);

        httppost.setEntity(mpEntity);
        
        try {
            response = httpClient.execute(httppost);
            HttpEntity resEntity = response.getEntity();      
            
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return response.toString();
Gibt es nun eine Möglichkeit die zwei zusammenzupacken?
Am liebsten wäre mir, wenn ich über das MultipartEntity auch Json übertragen kann (Bitte bei Vorschlägen auch gleich zeigen, wie ich das in PHP behandeln muss.)

Danke
 

Ähnliche Themen

M
  • MikelKatzengreis
Antworten
5
Aufrufe
127
swa00
swa00
Laser5001
Antworten
3
Aufrufe
650
swa00
swa00
W
Antworten
2
Aufrufe
744
rene3006
R
Zurück
Oben Unten