有没有办法像标题那样发送数据?
我知道如何从TextView
或EditText
发送数据,我只需要向服务器插入一个数据,但是要从具有多个数据的JsonArray
发送数据?我不知道如何让它发挥作用。
我在JsonArray
中使用此代码创建了button.onClick
并将其设置为TextView
,因此我知道JsonArray
的输出:
int size = adapter3.getCount();
StringBuilder b = new StringBuilder();
JSONArray jArray = new JSONArray();
for (int i=0;i<=size;i++){
jArray.put(adapter3.getItem(i).getJsonObject());
}
b.append(jArray);
TextView Star = (TextView)findViewById(R.id.test);
Star.setText(b.toString());
然后输出似乎:
[{"id":"1","nilai":"1"},{"id":"2","nilai":"1"},{"id":"3","nilai":"1"},{"id":"4","nilai":"1"},{"id":"5","nilai":"1"}]
现在我不知道如何通过PHP将此JsonArray
发送到MySQL,所以我需要帮助。
完整代码:
MainActivity.java
public class MainActivity2 extends AppCompatActivity {
ListView listView;
ArrayList < Pertanyaan > listPertanyaan;
ArrayAdapter < Pertanyaan > adapter3;
Button kirim;
TextView dos;
TextView kul;
ProgressDialog pDialog;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tanya);
Intent intent = getIntent();
String id = intent.getStringExtra("dosen");
String mat = intent.getStringExtra("matkul");
dos = (TextView) findViewById(R.id.dosen);
kul = (TextView) findViewById(R.id.matakuliah);
dos.setText(id);
kul.setText(mat);
listView = (ListView) findViewById(R.id.list_view);
kirim = (Button) findViewById(R.id.finish);
listPertanyaan = new ArrayList < > ();
getpertanyaan get = new getpertanyaan();
get.execute();
adapter3 = new PertanyaanAdapter(this, R.layout.item_listview, listPertanyaan);
listView.setAdapter(adapter3);
kirim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CreateNewProduct().execute();
int size = adapter3.getCount();
StringBuilder b = new StringBuilder();
JSONArray jArray = new JSONArray();
for (int i = 0; i <= size; i++) {
jArray.put(adapter3.getItem(i).getJsonObject());
}
b.append(jArray);
TextView Star = (TextView) findViewById(R.id.test);
Star.setText(b.toString());
}
});
}
Pertanyaan.java
public class Pertanyaan {
private String id;
private float ratingStar;
private String ask;
Pertanyaan(String id, int ratingStar, String ask) {
this.id = id;
this.ratingStar = ratingStar;
this.ask = ask;
}
public String getId() {
return id;
}
float getRatingStar() {
return ratingStar;
}
void setRatingStar(float ratingStar) {
this.ratingStar = ratingStar;
}
public String getAsk() {
return ask;
}
public JSONObject getJsonObject() {
JSONObject obj = new JSONObject();
try {
obj.put("id", id);
obj.put("nilai", ratingStar);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
}
PertanyaanAdapter.java
class PertanyaanAdapter extends ArrayAdapter<Pertanyaan> {
private AppCompatActivity activity;
private List<Pertanyaan> movieList;
PertanyaanAdapter(AppCompatActivity context, int resource, List<Pertanyaan> objects) {
super(context, resource, objects);
this.activity = context;
this.movieList = objects;
}
@Override
public Pertanyaan getItem(int position) {
return movieList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_listview, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
//holder.ratingBar.getTag(position);
}
holder.ratingBar.setOnRatingBarChangeListener(onRatingChangedListener(position));
holder.ratingBar.setTag(position);
holder.ratingBar.setRating(getItem(position).getRatingStar());
holder.movieName.setText(getItem(position).getAsk());
holder.id.setText(getItem(position).getId());
return convertView;
}
private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final int position) {
return new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
Pertanyaan item = getItem(position);
assert item != null;
item.setRatingStar(v);
Log.i("Adapter", "star: " + v);
}
};
}
private static class ViewHolder {
private RatingBar ratingBar;
private TextView movieName;
private TextView id;
ViewHolder(View view) {
ratingBar = (RatingBar) view.findViewById(R.id.rate_img);
movieName = (TextView) view.findViewById(R.id.text);
id = (TextView) view.findViewById(R.id.textid);
}
}
}
答案 0 :(得分:0)
发送帖子请求使用齐射库:
compile 'dev.dworks.libs:volleyplus:+'
谷歌搜索使用该库,这个我的代码发送多个帖子数据
String url = "url that handle the request";
final Context currentContext = CompleteDataActivity.this;
final StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// success handling code here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error handling code here
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> param = new HashMap<>();
//Post parameter
param.put("parameter1", jsonstring1);
param.put("parameter2", jsonstring2);
return param;
}
};
H.showLoadingDialog(currentContext);
MyVolley.getRequestQueue().add(request);
BTW,orang indo ya?
答案 1 :(得分:0)
你能做的是:
的Java / Android的
String jsonObjectString="{\"id\":1,\"nilai\":1}";
你可以使用一个循环来制作你想要的东西:
ArrayList<String> yourList=new Arrayist<>();
for(int i=0;i<5;i++)
{
yourList.add("{\"id\":"+(i+1)+",\"nilai\":1}");
}
然后使用Volley发送带有POST的ArrayList:
String url = "url that handle the request";
final Context currentContext = CompleteDataActivity.this;
final StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// success handling code here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error handling code here
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> param = new HashMap<>();
//Post parameter
for(int i=0;i<yourList.size();i++)
{
param.put("jsonObject"+i, yourList.get(i));
}
return param;
}
};
H.showLoadingDialog(currentContext);
MyVolley.getRequestQueue().add(request);
使用此代码获取.php
的JSONObject列表for($i=0;$i<5;$i++)
{
$var = "jsonObject".$i;
$jsonObject=json_decode($_POST[$var]);
$id = $jsonEtape->{'id'};
$nilai = $jsonEtape->{'nilai'};
$sql = "your query;";
mysqli_query($con,$sql);
}
希望这会有所帮助