2016-03-17 4 views
0
  1. Я новичок в Android и отдых Template.I разработали андроид экрана и отображения данных в формате JSON с помощью Rest шаблона GET метод на мой screen.My следующий шаг, чтобы обновить некоторые поля, такие как имя редактирования, и добавьте недостающие вещи и сохраните их обратно.Отдых Шаблон метод PUT В Android

    открытый класс MyPreferences extends AppCompatActivity { TextView tv; 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. Он отобразит экран, как на изображении.

<?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. Благодарен им.

ответ

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; 
    } 
} 

} 
+0

Проведя целый день, наконец, я нашел решение ............. – rakesh

Смежные вопросы