2013-09-21 2 views
0

내 안드로이드 애플리케이션의 데이터를 PHP 서버에 전달해야합니다.안드로이드 애플리케이션의 데이터를 서버로 전달

이 지금 내 서버에서 PHP 파일에서 JSON 객체로이 데이터를 읽을 필요가 내 코드

public class BackgroundDataLoader extends AsyncTask<Void, Void, String>{ 


@Override 
protected String doInBackground(Void... params) { 

    JSONObject jsObj=new JSONObject(); 

    try { 
     jsObj.put("ID", 1); 
     jsObj.put("Name", "Shashika"); 
    } catch (JSONException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url+"/data.php"); 


    try { 
     StringEntity se=new StringEntity(jsObj.toString()); 
     se.setContentType("application/json;charset=UTF-8"); 
     se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); 
     httppost.setEntity(se); 

    } catch (UnsupportedEncodingException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

     JSONArray finalResult = null; 
     String json = null; 
     HttpResponse response = null; 
     String text = null; 
     JSONArray jsonArray; 
     JSONObject jsonObject = null; 

     // Execute HTTP Post Request 

     try { 

      response = httpclient.execute(httppost); 
      int statusCode=response.getStatusLine().getStatusCode(); 

      if(statusCode==200){ 

       HttpEntity entity=response.getEntity(); 
       text=EntityUtils.toString(entity); 
      } 

      else{ 

       return "error "+response.getStatusLine().getStatusCode(); 
      } 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 

      e.printStackTrace(); 

     } catch (IOException e) { 

      // TODO Auto-generated catch block 

      e.printStackTrace(); 
     } 

     try { 
      jsonArray= new JSONArray(text); 
      //text=jsonArray.getJSONObject(0).getJSONArray(name); 
      text=jsonArray.getString(0); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     String tag=text; 
     Log.d(tag, text); 
     return text; 

} 

입니다. 어떻게 서버에서 내 PHP 파일에서 이러한 데이터를 읽을 수 있습니까?

+0

PHP 스크립트를 아직 만들지 않았습니까 – kabuto178

+0

아니요, 실제로 그 방법을 모릅니다 – Shashika

+0

PHP 스크립트 작성 방법을 모르십니까? – Jazib

답변

0

PHP에 집중하고 안드로이드 코드가 아니라면 실제로 좋을 것입니다. 따라서 일반적인 HTTP 요청에 따라 JSON 데이터를 POST하는 것으로 가정합니다. 그렇지 않은 경우 명시하십시오.

다음 코드와 행복해야한다 :

// get the POSTed data 
$json = file_get_contents('php://input'); 

// decode the JSON formatted data 
$obj = json_decode($json); 

// such that - on success - $obj is either null,true,false, an array or a stdClass object 

// assuming you POST {"my_key":"my_value"} you can access this as follows 
$obj->my_key == 'my_value'; // -> true 

// or if you pass the according Options to json_decode to enforce using associative arrays 
$obj['my_key'] == 'my_value'; // ->true 

은 기본적으로 당신이 우연히 '의 PHP JSON'와 구글의 첫 번째 히트되고, 공식 PHP JSON documentation에서 자세한 내용을 확인할 수 있습니다.

나는 PHP에서 기본적인 코딩을 수행하는 방법을 알고 있다고 가정합니다.

관련 문제