以下是参考代码:
数据在本地进入数据库,我在服务器上复制它,从我的设备访问应用程序,该设备使用API级别23,这也是我的gradle文件中的目标sdk。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_sugarcane_details_layout);
final EditText etyear = (EditText) findViewById(R.id.textYear);
final EditText etSpecies = (EditText) findViewById(R.id.textSpecies);
final EditText etPlantingDate = (EditText) findViewById(R.id.textPlantingdate);
final EditText etPlantingArea = (EditText) findViewById(R.id.textPlantingarea);
final EditText etProduction = (EditText) findViewById(R.id.textProduction);
final EditText etExpectedAmount = (EditText) findViewById(R.id.textExpectedAmount);
final EditText etSabhasadpart = (EditText) findViewById(R.id.textSabhasadpart);
final EditText etWhere = (EditText) findViewById(R.id.textWhere);
final EditText etDistance = (EditText) findViewById(R.id.textDistance);
final EditText ettransportmode = (EditText) findViewById(R.id.texttransportmode);
final EditText ettransportcost = (EditText) findViewById(R.id.texttransportcost);
final EditText etothersupply = (EditText) findViewById(R.id.textothersupply);
final Button bSave = (Button) findViewById(R.id.btnSavedetails);
bSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String year = etyear.getText().toString();
final String species = etSpecies.getText().toString();
final String pdate = etPlantingDate.getText().toString();
final int parea = Integer.parseInt(etPlantingArea.getText().toString());
final int production = Integer.parseInt(etProduction.getText().toString());
final int expamt = Integer.parseInt(etExpectedAmount.getText().toString());
final String sabhasadpart = etSabhasadpart.getText().toString();
final String where = etWhere.getText().toString();
final int distance = Integer.parseInt(etDistance.getText().toString());
final String transportmode = ettransportmode.getText().toString();
final int transportcost = Integer.parseInt(ettransportcost.getText().toString());
final String othersupply = etothersupply.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(EnterSugarcaneDetails.this, UserAreaActivity.class);
EnterSugarcaneDetails.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(EnterSugarcaneDetails.this);
builder.setMessage("Details were not updated")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
DetailsRequest detailsRequest = new DetailsRequest(year, species, pdate, parea, production, expamt, sabhasadpart, where, distance, transportmode, transportcost, othersupply, responseListener);
RequestQueue queue = Volley.newRequestQueue(EnterSugarcaneDetails.this);
queue.add(detailsRequest);
}
});
}
}
答案 0 :(得分:0)
menifest
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
还注册了
UserAreaActivity.class注册到您的清单
yourActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_sugarcane_details_layout);
final EditText etyear = (EditText) findViewById(R.id.textYear);
final EditText etSpecies = (EditText) findViewById(R.id.textSpecies);
final EditText etPlantingDate = (EditText) findViewById(R.id.textPlantingdate);
final EditText etPlantingArea = (EditText) findViewById(R.id.textPlantingarea);
final EditText etProduction = (EditText) findViewById(R.id.textProduction);
final EditText etExpectedAmount = (EditText) findViewById(R.id.textExpectedAmount);
final EditText etSabhasadpart = (EditText) findViewById(R.id.textSabhasadpart);
final EditText etWhere = (EditText) findViewById(R.id.textWhere);
final EditText etDistance = (EditText) findViewById(R.id.textDistance);
final EditText ettransportmode = (EditText) findViewById(R.id.texttransportmode);
final EditText ettransportcost = (EditText) findViewById(R.id.texttransportcost);
final EditText etothersupply = (EditText) findViewById(R.id.textothersupply);
final Button bSave = (Button) findViewById(R.id.btnSavedetails);
if(isNetworkConnected)
{
bSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String year = etyear.getText().toString();
final String species = etSpecies.getText().toString();
final String pdate = etPlantingDate.getText().toString();
final int parea = Integer.parseInt(etPlantingArea.getText().toString());
final int production = Integer.parseInt(etProduction.getText().toString());
final int expamt = Integer.parseInt(etExpectedAmount.getText().toString());
final String sabhasadpart = etSabhasadpart.getText().toString();
final String where = etWhere.getText().toString();
final int distance = Integer.parseInt(etDistance.getText().toString());
final String transportmode = ettransportmode.getText().toString();
final int transportcost = Integer.parseInt(ettransportcost.getText().toString());
final String othersupply = etothersupply.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(EnterSugarcaneDetails.this, UserAreaActivity.class);
EnterSugarcaneDetails.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(EnterSugarcaneDetails.this);
builder.setMessage("Details were not updated")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
DetailsRequest detailsRequest = new DetailsRequest(year, species, pdate, parea, production, expamt, sabhasadpart, where, distance, transportmode, transportcost, othersupply, responseListener);
RequestQueue queue = Volley.newRequestQueue(EnterSugarcaneDetails.this);
queue.add(detailsRequest);
}
});}else{
Toast.makeText(getApplicationContext(),"chec your internet connection", Toast.LENGTH_LONG).show();
}
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}