当前位置: 代码迷 >> Android >> 使用齐射在服务器上发布数据
  详细解决方案

使用齐射在服务器上发布数据

热度:84   发布时间:2023-08-04 11:26:14.0

我正在使用Volley在服务器上发送一些数据。 另外,我有一个Java类名称SpeakerBean ,在其中定义了所有其值来自服务器的参数,并在其中将连接了不同类名称的值发布到服务器上。 现在我的问题是,在连接类我要送的价值之一SpeakerBeanspeaker_id与连接类的参数内部,但我不知道怎么做,因为我不使用控制/部件(文本框,编辑框)对于speaker_id 那么,如何使用我发布到服务器的值传递此ID?

我的代码:

这是连接类以在服务器上发布值

 public class ConnectWithSpeaker extends AppCompatActivity implements View.OnClickListener {

 LinearLayout linear_layoutcontainer;
 Toolbar toolbar;


 String url = Constants.SUBMIT_API;
 public static final String KEY_NAME = "name";
 public static final String KEY_EMAIL = "email";
 public static final String KEY_MOBILE = "mobile";
 public static final String KEY_COMPANY = "company";
 public static final String KEY_SPEAKERID = "speaker_id";

 String s_id;
 private EditText u_name;
 private EditText u_email;
 private EditText u_mobile;
 private EditText u_company;
 private Button submit;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_connect_with_speaker);
     linear_layoutcontainer = (LinearLayout)   findViewById(R.id.linear_layoutcontainer);
     toolbar = (Toolbar) findViewById(R.id.customtoolbar);
     TextView title = (TextView) toolbar.findViewById(R.id.title);
     title.setText("Connect with Speakers");
     toolbar.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             finish();
         }
     });
     u_name = (EditText) findViewById(R.id.name);
     u_email = (EditText) findViewById(R.id.email);
     u_mobile = (EditText) findViewById(R.id.mobile);
     u_company = (EditText) findViewById(R.id.compny_name);
     submit = (Button) findViewById(R.id.connect);
     submit.setOnClickListener(this);
 }

 private void submitdetails() {
     final String name = u_name.getText().toString().trim();   //trim()  remove spaces after&before string
     final String email = u_email.getText().toString().trim();
     final String mobile = u_mobile.getText().toString().trim();
     final String company = u_company.getText().toString().trim();
     final String speaker_id =s_id;


     SpeakersBean sb_obj=new SpeakersBean();
        sb_obj = getIntent().getParcelableExtra("speaker");
        sb_obj.setSpeaker_id(sb_obj.getSpeaker_id().toString().trim());   
       //getting null
       // sb_obj.setSpeaker_id(s_id);
       //s_id =sb_obj.getSpeaker_id();

     Toast.makeText(ConnectWithSpeaker.this, "submit details",  Toast.LENGTH_SHORT).show();
     CustomJSONObjectRequest request2 = new   CustomJSONObjectRequest(Request.Method.POST, url, new
 Response.Listener<String>() {
         @Override
         public void onResponse(String response) {
             if (response.trim().equals("success")) {
                 Toast.makeText(getApplicationContext(), "Your request is proceed, we will update you with further updates.",
  Toast.LENGTH_LONG).show();
             }
         }
     }, new Response.ErrorListener() {
                 @Override
                 public void onErrorResponse(VolleyError volleyError) {
                     Toast.makeText(ConnectWithSpeaker.this,  volleyError.toString(), Toast.LENGTH_LONG).show();
                 }
             }) {
         @Override
         protected Map<String, String> getParams() {
             Map<String, String> params = new HashMap<String, String>();
             params.put(KEY_NAME, name);
             params.put(KEY_EMAIL, email);
             params.put(KEY_MOBILE, mobile);
             params.put(KEY_COMPANY, company);
            // params.put(KEY_SPEAKERID, speaker_id);   //pass speaker id too with these parameters coming from speaker_api
             return params;
         }
     };
     RequestQueue requestQueue = Volley.newRequestQueue(this);
     requestQueue.add(request2);
 }
 @Override
 public void onClick(View v) {
     if (v == submit)
     {
         if (TextUtils.isEmpty(u_name.getText().toString())) {
            // u_name.setError("Enter Name");
             u_name.requestFocus();
         }
         if (TextUtils.isEmpty(u_email.getText().toString()))
         {
            // u_email.setError("Enter Email");
             u_email.requestFocus();
         }
         if((TextUtils.isEmpty(u_mobile.getText().toString()))||(u_mobile.length()<10||u_mobile.length()>15))
         {
             u_mobile.setError("Enter valid Mobile No");
             u_mobile.requestFocus();
         }
         if (TextUtils.isEmpty(u_company.getText().toString()))
         {
            // u_company.setError("Enter Company Name");
             u_company.requestFocus();
         } else {
             submitdetails();
         }
    /* if(v==submit){
     submitdetails();
     }*/

     }
 } }

这是我的SpeakerBean

 public class SpeakersBean implements Parcelable{
 @SerializedName("speaker_id")
 private String speaker_id;

 @SerializedName("name")
 private String name;

 @SerializedName("company")
 private String company;

 @SerializedName("designation")
 private String designation;

 @SerializedName("thumbnail")
 private String thumbnail;

 @SerializedName("original_image")
 private String original_image;


 @SerializedName("speaker_info")
 private String speaker_info;

 @SerializedName("twitter_url")
 private String twitter_url;

 @SerializedName("linkdin_url")
 private String linkdin_url;

 @SerializedName("facebook_url")
 private String facebook_url;

 @SerializedName("last_sync_time")
 private long last_sync_time;

 public long getLast_sync_time() {
     return last_sync_time;
 }

 public void setLast_sync_time(long last_sync_time) {
     this.last_sync_time = last_sync_time;
 }

 public String getSpeaker_id() {
     return speaker_id;
 }

 public void setSpeaker_id(String speaker_id) {
     this.speaker_id = speaker_id;
 }

 public String getName() {
     return name;
 }

 public void setName(String name) {
     this.name = name;
 }

 public String getDesignation() {
     return designation;
 }

 public void setDesignation(String designation) {
     this.designation = designation;
 }

 public String getCompany() {
     return company;
 }

 public void setCompany(String company) {
     this.company = company;
 }

 public String getThumbnail() {
     return thumbnail;
 }

 public void setThumbnail(String thumbnail) {
     this.thumbnail = thumbnail;
 }

 public String getOriginal_image() {
     return original_image;
 }

 public void setOriginal_image(String original_image) {
     this.original_image = original_image;
 }

 public String getSpeaker_info() {
     return speaker_info;
 }

 public void setSpeaker_info(String speaker_info) {
     this.speaker_info = speaker_info;
 }

 public String getTwitter_url() {
     return twitter_url;
 }

 public void setTwitter_url(String twitter_url) {
     this.twitter_url = twitter_url;
 }

 public String getLinkdin_url() {
     return linkdin_url;
 }

 public void setLinkdin_url(String linkdin_url) {
     this.linkdin_url = linkdin_url;
 }

 public String getFacebook_url() {
     return facebook_url;
 }

 public void setFacebook_url(String facebook_url) {
     this.facebook_url = facebook_url;
 }

 public SpeakersBean(){

 }
 // Parcelling part
 public SpeakersBean(Parcel in){
     String[] data = new String[10];

     in.readStringArray(data);
     this.speaker_id = data[0];
     this.name = data[1];
     this.company = data[2];
     this.designation = data[3];
     this.thumbnail = data[4];
     this.original_image=data[5];
     this.speaker_info = data[6];
     this.twitter_url = data[7];
     this.linkdin_url = data[8];
     this.facebook_url = data[9];
 }

 @Override
 public int describeContents() {
     return 0;
 }

 @Override
 public void writeToParcel(Parcel dest, int flags) {
     dest.writeStringArray(new String[] {this.speaker_id,
             this.name,
             this.company,
             this.designation,
             this.thumbnail,
             this.original_image,
             this.speaker_info,
             this.twitter_url,
             this.linkdin_url,
             this.facebook_url});
 }
 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
     public SpeakersBean createFromParcel(Parcel in) {
         return new SpeakersBean(in);
     }
     public SpeakersBean[] newArray(int size) {
         return new SpeakersBean[size];
     }
 }; }

这是我用于连接的布局资源文件

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >      <ScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      <include
          android:id="@+id/customtoolbar"
          layout="@layout/customtoolbar_youtube" />

 <LinearLayout
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="20dp"
 android:layout_marginTop="50dp"
 android:layout_marginRight="20dp"
 android:layout_marginBottom="50dp"
 android:background="@color/dark_gray">
 <EditText
     android:singleLine="true"
     android:id="@+id/name"
     android:layout_marginTop="20dp"
     android:layout_marginLeft="20dp"
     android:layout_marginRight="20dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Name"/>
 <EditText
     android:singleLine="true"
     android:id="@+id/email"
     android:layout_marginTop="20dp"
     android:layout_marginLeft="20dp"
     android:layout_marginRight="20dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Email"/>
 <EditText
     android:singleLine="true"
     android:id="@+id/mobile"
     android:layout_marginTop="20dp"
     android:layout_marginLeft="20dp"
     android:layout_marginRight="20dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Mobile"/>
 <EditText
     android:singleLine="true"
     android:id="@+id/compny_name"
     android:layout_marginTop="20dp"
     android:layout_marginLeft="20dp"
     android:layout_marginRight="20dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Company Name"/>
 <Button
     android:id="@+id/connect"
     android:layout_width="wrap_content"
     android:layout_height="40dp"
     android:background="@drawable/roundbutton"
     android:layout_gravity="center"
     android:text="Submit"
     android:textColor="@color/white"
     android:layout_marginBottom="20dp"/> </LinearLayout>
    </LinearLayout>
   </ScrollView>

   </RelativeLayout>

告诉我如何在此处为speaker_id使用get和set。 由于我不明白我在连接类中使用的代码在这里

   SpeakersBean sb_obj=new SpeakersBean();
    sb_obj = getIntent().getParcelableExtra("speaker");
    sb_obj.setSpeaker_id(sb_obj.getSpeaker_id().toString().trim());   
   //getting null
   // sb_obj.setSpeaker_id(s_id);
   //s_id =sb_obj.getSpeaker_id();

试试这个,使用排球获取数据并检查是否正在获取数据

之后发布此

  相关解决方案