SORU
12 HAZİRAN 2010, CUMARTESİ


Nasıl bir JSON nesnesi göndermek için Android ile İstek üzerine?

JSON aşağıdaki metin {"Email":"aaa@tbbb.com","Password":"123456"} bir web hizmetine göndermek ve yanıtı okumak istiyorum. JSON okumayı biliyorum. Sorun JSON yukarıdaki nesne bir değişken adı jason gönderilmesi gerekir.

Nasıl android olabilir mi? Oluşturma isteği gibi adımlar nelerdir, içerik başlıkları, vb ayarı nesne.

CEVAP
12 HAZİRAN 2010, CUMARTESİ


Android json nesnesi göndermek eğer Apache HTTP Client kullanırsanız kolay olur. İşte bunu yapmak için nasıl bir kod örneği. UI iş parçacığı kilitlemek için ağ faaliyetleri için yeni bir iş parçacığı oluşturmanız gerekir.

    protected void sendJson(final String email, final String pwd) {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();

                try {
                    HttpPost post = new HttpPost(URL);
                    json.put("email", email);
                    json.put("password", pwd);
                    StringEntity se = new StringEntity( json.toString());  
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);

                    /*Checking response */
                    if(response!=null){
                        InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                    createDialog("Error", "Cannot Estabilish Connection");
                }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();      
    }

Ayrıca Google Gson JSON göndermek ve almak için kullanabilirsiniz.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • alex maybury

    alex maybury

    20 Aralık 2007
  • Missouri Star Quilt Company

    Missouri Sta

    18 ŞUBAT 2009
  • monkophile

    monkophile

    25 Temmuz 2007