JAVA/Android

네트워크(Network) : xml [xml, json]

zammanza 2013. 4. 9. 12:47

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="연결 요청" />

 

    <ScrollView

        android:id="@+id/scrollView1"

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

 

        <TextView

            android:id="@+id/textView1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />

 

    </ScrollView>

 

</LinearLayout>

 

 

 

MainActivity.java

public class MainActivity extends Activity {

        

         ProgressDialog progressDialog;

        

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                  super.onCreate(savedInstanceState);

                  setContentView(R.layout.activity_main);

                 

                  findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

                          

                           @Override

                           public void onClick(View v) {

                                   // TODO Auto-generated method stub

                                   readServer();

                           }

                  });

         }

 

         @Override

         public boolean onCreateOptionsMenu(Menu menu) {

                  // Inflate the menu; this adds items to the action bar if it is present.

                  getMenuInflater().inflate(R.menu.main, menu);

                  return true;

         }

        

         private void readServer(){

                  Thread thread = new Thread(new Runnable() {

                          

                           @Override

                           public void run() {

                                   // TODO Auto-generated method stub

                                   try {

                                            //String url = "http://maps.googleapis.com/maps/api/geocode/xml?address=%EC%84%9C%EC%9A%B8%EC%8B%9C%EC%B2%AD&sensor=true";    //xml로 받기

                                           String url = "http://maps.googleapis.com/maps/api/geocode/json?address=%EC%84%9C%EC%9A%B8%EC%8B%9C%EC%B2%AD&sensor=true";    //json으로 받기

                                           

                                           

                                            // 1.Get방식

                                            HttpGet get = new HttpGet(url);

                                            HttpResponse responseGET = new DefaultHttpClient().execute(get);

                                           

                                            // 2.Post방식

                                            //HttpPost post = new HttpPost(url);

                                            //List<NameValuePair> params = new ArrayList<NameValuePair>();

                                           

                                            // 값을 입력

                                   /*       params.add(new BasicNameValuePair("변수1", "1"));

                                            params.add(new BasicNameValuePair("변수2", "2"));

                                            params.add(new BasicNameValuePair("변수3", "3"));

                                           

                                            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

                                            HttpResponse responseGET = new DefaultHttpClient().execute(post);*/

                                           

                                           

                                            HttpEntity resEntity = responseGET.getEntity();

                                            if(resEntity != null){

                                                     String resString = EntityUtils.toString(resEntity, "UTF-8");

                                                    

                                                     Message message = handler.obtainMessage();

                                                     message.obj = resString;

                                                     handler.sendMessage(message);

                                            }

                                   } catch (ClientProtocolException e) {

                                            // TODO Auto-generated catch block

 

                                   } catch (ParseException e) {

                                            // TODO Auto-generated catch block

                                           

                                   } catch (IOException e) {

                                            // TODO Auto-generated catch block

                                           

                                   }

                           }

                  });

                 

                  progressDialog = ProgressDialog.show(MainActivity.this, "Wite", "Downloading...");

                 

                  thread.start();

         }

        

         private Handler handler = new Handler(){

 

                  @Override

                  public void handleMessage(Message msg) {

                           // TODO Auto-generated method stub

                          

                           progressDialog.dismiss();

                          

                           TextView textView1 = (TextView)findViewById(R.id.textView1);

                           textView1.setText((String)msg.obj);

                  }

                 

         };

}