【问题标题】:Android Edit Text and Save To Http ServerAndroid 编辑文本并保存到 Http 服务器
【发布时间】:2016-03-16 10:26:38
【问题描述】:
package com.nusecond.suredeal.app.suredeal.activity;

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.util.Log;
import android.view.View;
import android.widget.TextView;

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

import org.springframework.web.client.RestTemplate;

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

public class MyPreferences extends AppCompatActivity {
    private List<ConsumerProfile>cp = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_preferences);
        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());

        }

    }

}
  1. 这是在我的 Android 应用中显示 JSON 数据的活动。
  2. 我的主要目标是允许用户编辑和更新详细信息。
  3. 到目前为止,我可以将 Http 服务中的数据显示到我的应用程序中。
  4. 下一步是允许用户编辑详细信息并保存回服务。
  5. 我是 android 新手,感谢他们的帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Update Preferences"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />


</LinearLayout>
  1. 这是我的布局文件。
  2. 编辑首选项后。
  3. 如果选择了更新按钮,它应该更新回服务器。
  4. 还有如何添加垂直滚动视图。
  5. 非常感谢任何帮助。

This is the screen I'm getting now

【问题讨论】:

  • 我建议你使用 Retrofit 而不是 RestTemplate。节省大量时间。因此,对于您的问题 - 在服务器端创建一个 POST 或 PUT 端点,然后准备将这些数据发送到服务器的请求。 square.github.io/retrofit
  • put方法的终点和get方法一样 url@Nekromancer

标签: android json rest http


【解决方案1】:

1) 在更新按钮上单击启动 httppost 请求,该请求负责将数据发送到服务器..

你可以解析jsonobject中的所有值

    String url= "YOUR URL";

    JSONObject data=new JSONObject();
    try {
        data.put("firstname", "abc");
        data.put("lastname", "xyz");
        data.put("dob", "11-03-2016");
        data.put("status", "ac");
        data.put("homeaddress", "cac");

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

    HttpResponse response = null;
        HttpPost httppostreq = new HttpPost(url);
        try {
            StringEntity se = new StringEntity(data.toString());
            se.setContentType("application/json;charset=UTF-8"); 
            httppostreq.setEntity(se);
            response = client.execute(httppostreq);
            HttpEntity entity = response.getEntity(); /*.. read entity here..*/
        }
        catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;

        }catch(Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return false;
        }

2) 对于滚动视图

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

<!-- your layout element -->
.....
.....
.....

</LinearLayout>

</ScrollView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2011-02-19
    • 2015-05-26
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多