Rest模板PUT方法在Android中

时间:2016-03-17 06:34:23

标签: android rest

  1. 我是Android和Rest Template的新手。我开发了一个Android 使用Rest Template GET方法在我的屏幕上显示JSON数据 我的下一步是更新一些字段,如编辑名称和添加 丢失的东西并保存回其余模板。

    public class MyPreferences扩展AppCompatActivity {     TextView电视;     private Listcp = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_preferences);
        Resources resources = getResources();
        Log.d("Consumer pojo", "onCreate:");
        new HttpRequestTask().execute();
    
    
    
    }
    private class HttpRequestTask extends AsyncTask<Void, Void, ConsumerProfile>{
    
    
    
        @Override
        protected ConsumerProfile doInBackground(Void... params) {
            try {
    
                final String url = "http://192.168.1.213:9001/consumer/local/"+LoginFragment.CONSUMEROBJECT.getId();
                RestTemplate restTemplate = new RestTemplate();
                ConsumerProfile cp = restTemplate.getForObject(url, ConsumerProfile.class);
    
                return cp;
            }catch (Exception e){
                Log.e("MainActivity", e.getMessage(),e );
    
            }
            return null;
        }
        @Override
        protected void onPostExecute(ConsumerProfile cp){
            super.onPostExecute(cp);
            Log.d("cppppppppppppppppppppp", "onPostExecute: " + cp.getId());
    
            TextView fname=(TextView)findViewById(R.id.editfname);
            TextView mname=(TextView)findViewById(R.id.editmname);
            TextView lname=(TextView)findViewById(R.id.editlname);
            TextView nname=(TextView)findViewById(R.id.editnname);
            TextView dob=(TextView)findViewById(R.id.editdob);
            TextView status=(TextView)findViewById(R.id.editstatus);
            TextView homeAddress=(TextView)findViewById(R.id.edithomeAddr);
            TextView workAddress=(TextView)findViewById(R.id.editworkAddr);
            TextView income=(TextView)findViewById(R.id.editincome);
            fname.setText(cp.getFirstName());
            mname.setText(cp.getMiddleName());
            lname.setText(cp.getLastName());
            nname.setText(cp.getNickName());
            dob.setText(cp.getDob());
            status.setText(cp.getStatus());
            homeAddress.setText(cp.getHomeAddress());
            workAddress.setText(cp.getWorkAddress());
            income.setText(cp.getIncome());
    

    } } This Screen is for Get Method

  2. 这就是我现在做的事情。

  3. 此代码适用于Get方法。

  4. 它会像图像一样显示屏幕。
  5. <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"><![CDATA[
    
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
    
    
        ]]>
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/scrollView2" >
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">    <TextView
                android:layout_width="wrap_content"
                android:scrollbars="vertical"
                android:layout_height="wrap_content"
                android:text="firstName"/>
    
                <EditText
                    android:id="@+id/editfname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="middleName"/>
                <EditText
                    android:id="@+id/editmname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="lastName"/>
                <EditText
                    android:id="@+id/editlname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="nickName"/>
                <EditText
                    android:id="@+id/editnname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="dob"/>
                <EditText
                    android:id="@+id/editdob"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="status"/>
                <EditText
                    android:id="@+id/editstatus"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="homeAddress"/>
                <EditText
                    android:id="@+id/edithomeAddr"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="workAddress"/>
                <EditText
                    android:id="@+id/editworkAddr"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <TextView
    
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="income"/>
                <EditText
                    android:id="@+id/editincome"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    
                <Button
                    android:scrollbars="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Update Preferences"
                    android:id="@+id/button"
                    android:layout_gravity="center_horizontal" />
            </LinearLayout>
        </ScrollView>
    
    
    </LinearLayout>

    1. 这是我的布局。
    2. 实际上我要做的是获取用户信息和编辑信息并保存回相同的终点。
    3. 当用户选择更新按钮然后我将显示更新的屏幕。
    4. 任何帮助感谢。
    5. 任何人都为此提供代码。
    6. 感谢他们。

1 个答案:

答案 0 :(得分:0)

package com.nusecond.suredeal.app.suredeal.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.net.http.HttpResponseCache;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.pojo.Consumer;
import com.nusecond.suredeal.app.suredeal.pojo.ConsumerProfile;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

public class MyPreferences extends AppCompatActivity {
    Button button;
    TextView tv;
    TextView fname,mname,lname,nname,dob,status,homeAddress,workAddress,income;
    private  String FName,MName,LName,NName,Dob,Status,HomeAddress,WorkAddress,Income;
    private List<ConsumerProfile>cp = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_preferences);
        Resources resources = getResources();
        Log.d("Consumer pojo", "onCreate:");
        new HttpRequestTask().execute();
        addListenerOnButton();



    }

    private void addListenerOnButton() {
        //final String url = "http://192.168.1.213:9001/consumer/local/"+LoginFragment.CONSUMEROBJECT.getId();
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FName=fname.getText().toString();
                MName=mname.getText().toString();
                LName=lname.getText().toString();
                NName=nname.getText().toString();
                Dob=dob.getText().toString();
                Status=status.getText().toString();
                HomeAddress=homeAddress.getText().toString();
                WorkAddress=workAddress.getText().toString();

                new HttpUpdateTask().execute();
               // Toast.makeText(addListenerOnButton(),"UpdatedSuccessfully",Toast.LENGTH_LONG).show();
                Intent intent=new Intent(MyPreferences.this,MainActivity.class);
                startActivity(intent);

            }

        });
    }

    private class HttpRequestTask extends AsyncTask<Void, Void, ConsumerProfile>{



        @Override
        protected ConsumerProfile doInBackground(Void... params) {
            try {

                final String url = "http://192.168.1.213:9001/consumer/local/"+LoginFragment.CONSUMEROBJECT.getId();
                RestTemplate restTemplate = new RestTemplate();
                ConsumerProfile cp = restTemplate.getForObject(url, ConsumerProfile.class);

                return cp;

            }catch (Exception e){
                Log.e("MainActivity", e.getMessage(),e );

            }
            return null;
        }
        @Override
        protected void onPostExecute(ConsumerProfile cp){
            super.onPostExecute(cp);
            Log.d("cppppppppppppppppppppp", "onPostExecute: " + cp.getId());

            fname=(TextView)findViewById(R.id.editfname);
             mname=(TextView)findViewById(R.id.editmname);
             lname=(TextView)findViewById(R.id.editlname);
             nname=(TextView)findViewById(R.id.editnname);
             dob=(TextView)findViewById(R.id.editdob);
             status=(TextView)findViewById(R.id.editstatus);
             homeAddress=(TextView)findViewById(R.id.edithomeAddr);
             workAddress=(TextView)findViewById(R.id.editworkAddr);
             income=(TextView)findViewById(R.id.editincome);
            fname.setText(cp.getFirstName());
            mname.setText(cp.getMiddleName());
            lname.setText(cp.getLastName());
            nname.setText(cp.getNickName());
            dob.setText(cp.getDob());
            status.setText(cp.getStatus());
            homeAddress.setText(cp.getHomeAddress());
            workAddress.setText(cp.getWorkAddress());
            income.setText(cp.getIncome());
            //FName=fname.getText().toString();


        }







    }
private class HttpUpdateTask extends AsyncTask<Void,Void,ConsumerProfile>{

    @Override
    protected ConsumerProfile doInBackground(Void... params) {
       try {
           final String url = "http://192.168.1.213:9001/consumer/local/" + LoginFragment.CONSUMEROBJECT.getId();
           RestTemplate restTemplate = new RestTemplate();
           ConsumerProfile consumerProfile = new ConsumerProfile();




          // String m=FName;
           consumerProfile.setFirstName(FName);

           fname=(TextView)findViewById(R.id.editfname);
           Log.d(" llllllllllllll", "doInBackground: " + FName);
           consumerProfile.setMiddleName(MName);
           consumerProfile.setLastName(LName);
           consumerProfile.setNickName(NName);
           consumerProfile.setDob(Dob);
           consumerProfile.setStatus(Status);
           consumerProfile.setHomeAddress(HomeAddress);
           consumerProfile.setWorkAddress(WorkAddress);
           consumerProfile.setDob(Income);

           restTemplate.put(url,consumerProfile);
           Log.d("ettttttttttttttt", "doInBackground: ");

           return consumerProfile;
       }catch (Exception e){
           Log.e( "consumer", e.getMessage(),e);
       }
        return null;
    }
}

}