2016-04-01 20 views
2

Testten URL- "http://www.sample.com/interntest.php" { "şifre": "test12345"} gibi bir yük ile bir POST isteği yapmaya çalışıyorumkullanarak POST isteği httpConnectionUrl yaparken JSON biçiminde yükü vermek nasıl

public class VisitorActivity extends AppCompatActivity { 

private BitmapDrawable bitmapObject; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_visitor); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    BitmapDrawable bitmapObject=null; 
    new JSONTask().execute("http://www.sample.com/interntest.php"); 
    LinearLayout i = (LinearLayout)findViewById(R.id.backgroundImg); 
    if (i != null) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      i.setBackground(bitmapObject); 
     } 
    } 

} 

public class JSONTask extends AsyncTask<String, String, Bitmap> { 
    @Override 
    protected Bitmap doInBackground(String... params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 
     try { 
      URL url = new URL(params[0]); 
      connection= (HttpURLConnection) url.openConnection(); 
      String password="test12345"; 
      connection.setReadTimeout(10000); 
      connection.setConnectTimeout(15000); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      connection.setRequestMethod("POST"); 

      //connection.addRequestProperty("password", "test12345"); 
      DataOutputStream dbStrem= new DataOutputStream(connection.getOutputStream()); 
      dbStrem.writeBytes(password); 
      connection.connect(); 
      dbStrem.flush(); 
      dbStrem.close(); 
      InputStream stream=connection.getInputStream(); 
      reader=new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 
      String line=""; 

      //StringBuilder responseOutput = new StringBuilder(); 
      /*String password="test12345"; 
      responseOutput.append("password: "+password);*/ 

      System.out.println("output==============="); 

      while((line=reader.readLine())!=null) { 
       buffer.append(line); 
      } 
      String finalJSON= buffer.toString(); 
      JSONObject parentObject = new JSONObject(finalJSON); 
      Log.d("parentObject", String.valueOf(parentObject)); 
      String backgroundURL= parentObject.getString("backgroundURL"); 
      Log.d("IMGBACKGROUND",backgroundURL+backgroundURL); 
      Bitmap bitmap = null; 
      try { 
       bitmap = BitmapFactory.decodeStream((InputStream) new URL(backgroundURL).getContent()); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return bitmap; 
     } 
     catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if(connection!=null) { 
       connection.disconnect(); 
      } 
      if(reader!=null) { 
       try { 
        reader.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     super.onPostExecute(result); 
    } 
} 

}

Ancak burada httpUrlConnection içinde parametre ekleyemiyorum. HttpClient kullanarak nasıl param ekleyeceğinizi biliyorum, ancak artık desteklenmediği için kullanmak istemiyorum. Bunu yapmanın daha iyi bir yolunu bilebilir miyim?

+0

Volley kütüphanesi veya düzeltme kitaplığı kullanın, buradan istediğiniz gibi parametreler gönderebilirsiniz. – malli

+0

Kullanımdan kaldırıldı. Webservices- (Rest Api) –

+0

için Retrofit veya Volley kütüphanesini kullanmak, httpCLient kullanarak yaptığımız gibi bu kütüphaneleri kullanmadan mümkün değil miydi? –

cevap

0

Kullanım Volley hakkında bazı net bir fikir verir. Bu önerilen bir yaklaşım. Here belgelidir.