所以即时开发一个Android移动应用程序,可以将数据和图像上传到数据库。这一切都适用于我使用mysql和托管的数据库部分。我现在唯一的问题是当我使用wifi时,imageView会立即更新我上传的最新图像但是当我使用移动数据时,imageView不会刷新到我上传的最新图像。请告诉我如何修复此错误。 tqvm在高级
中1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.cliniclocum">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/caduceus_medicine_symbol_white_black_button"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainPageLogin"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegistrationPage"
android:label="@string/title_activity_registration_page" />
<activity
android:name=".ClinicOwnerMainPage"
android:label="@string/title_activity_clinic_owner_main_page" />
<activity
android:name=".DoctorMainPage"
android:label="@string/title_activity_doctor_main_page" />
<activity
android:name=".ClinicOwnerProfilePage"
android:label="@string/title_activity_clinic_owner_profile_page" />
<activity
android:name=".ClinicOwnerUploadImagePage"
android:label="@string/title_activity_clinic_owner_upload_image" />
<activity
android:name=".ClinicOwnerSetLocumJobPage"
android:label="@string/title_activity_clinic_owner_set_locum_job" />
<activity
android:name=".CoViewLocumPosted"
android:label="@string/title_activity_co_view_locum_posted" />
<activity
android:name=".CoUpdateLocumPostedDetails"
android:label="@string/title_activity_co_locum_posted_details" />
<activity
android:name=".DoProfilePage"
android:label="@string/title_activity_do_profile_page" />
<activity
android:name=".DoUploadApcImage"
android:label="@string/title_activity_do_upload_apc_image" />
<activity
android:name=".DoViewLocumPost"
android:label="@string/title_activity_do_view_locum_post" />
<activity
android:name=".DoViewLocumDetails"
android:label="@string/title_activity_do_view_locum_details" />
<activity
android:name=".DoViewClinicProfile"
android:label="@string/title_activity_do_view_clinic_profile" />
<activity android:name=".CoImageFullScreen" />
</application>
2.ProfilePage.java
public class ClinicOwnerProfilePage extends AppCompatActivity implements View.OnClickListener{
TextView textViewUserRole;
EditText editTextCoProfilePageName, editTextCoProfilePageAddress, editTextCoProfilePageClinicOwnerName, editTextCoProfileFacilities;
Button buttonCoProfileUploadClinicImage, buttonCoProfileUpdateProfile;
Spinner spinnerCoProfilePatientTurnover;
ArrayAdapter<CharSequence> adapter;
private ImageView imageViewCoProfilePageClinicImage;
private Bitmap bitmap;
String imagePath;
private ProgressDialog loading;
String receiveSpEmail;
String passedSpPatientTurnover;
// added for refreshing event
SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clinic_owner_profile_page);
textViewUserRole = (TextView)findViewById(R.id.tvCoUserRole);
editTextCoProfilePageName = (EditText)findViewById(R.id.etCoProfileName);
editTextCoProfilePageAddress = (EditText)findViewById(R.id.etCoProfileAddress);
editTextCoProfilePageClinicOwnerName = (EditText)findViewById(R.id.etCoProfileClinicOwnerName);
editTextCoProfileFacilities = (EditText)findViewById(R.id.etCoProfileFacilities);
buttonCoProfileUploadClinicImage = (Button)findViewById(R.id.btnCoProfileUploadImage);
buttonCoProfileUpdateProfile = (Button)findViewById(R.id.btnCoProfileUpdateProfile);
buttonCoProfileUploadClinicImage.setOnClickListener(this);
buttonCoProfileUpdateProfile.setOnClickListener(this);
spinnerCoProfilePatientTurnover = (Spinner)findViewById(R.id.spCoProfileUpdatePatientTurnover);
adapter = ArrayAdapter.createFromResource(this, R.array.SpinnerCoUpdatePatientTurnover, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCoProfilePatientTurnover.setAdapter(adapter);
spinnerCoProfilePatientTurnover.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
passedSpPatientTurnover = spinnerCoProfilePatientTurnover.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
receiveSpEmail();
getCoProfileData();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
imageViewCoProfilePageClinicImage = (ImageView) findViewById(R.id.ivCoProfileClinicImage);
//getCoImageProfile();
getImage();
/*imageViewCoProfilePageClinicImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// to send bitmap using intent, we need to change it into ByteArray first and putting byteArray into the intent extra
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 25, stream);
byte[] byteArray = stream.toByteArray();
Intent fullScreenIntent = new Intent(ClinicOwnerProfilePage.this, CoImageFullScreen.class);
fullScreenIntent.putExtra("CoImage", byteArray);
startActivity(fullScreenIntent);
}
});*/
//added for pull to refresh
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
getImage();
}
}, 3000);
}
});
//end of refreshing event
}
public void receiveSpEmail(){
SharedPreferences preferences = getSharedPreferences("PassingEmail", MODE_PRIVATE);
receiveSpEmail = preferences.getString("passEmail", "..");
textViewUserRole.setText(receiveSpEmail);
}
private void getCoProfileData() {
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = ClinicOwnerGetProfile.DATA_URL+receiveSpEmail.toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ClinicOwnerProfilePage.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String clinic_name="";
String address = "";
String clinicownername = "";
String facilities = "";
String patient_turnover = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(ClinicOwnerGetProfile.JSON_ARRAY);
JSONObject clinicOwnerData = result.getJSONObject(0);
clinic_name = clinicOwnerData.getString(ClinicOwnerGetProfile.KEY_NAME);
address = clinicOwnerData.getString(ClinicOwnerGetProfile.KEY_ADDRESS);
clinicownername = clinicOwnerData.getString(ClinicOwnerGetProfile.KEY_CLINICOWNERNAME);
facilities = clinicOwnerData.getString(ClinicOwnerGetProfile.KEY_FACILITIES);
patient_turnover = clinicOwnerData.getString(ClinicOwnerGetProfile.KEY_PATIENT_TURNOVER);
} catch (JSONException e) {
e.printStackTrace();
}
editTextCoProfilePageName.setText(clinic_name);
editTextCoProfilePageAddress.setText(address);
editTextCoProfilePageClinicOwnerName.setText(clinicownername);
editTextCoProfileFacilities.setText(facilities);
for (int i = 0; i < spinnerCoProfilePatientTurnover.getCount(); i++) {
if (spinnerCoProfilePatientTurnover.getItemAtPosition(i).equals(patient_turnover)) {
spinnerCoProfilePatientTurnover.setSelection(i);
break;
}
}
}
/*//added for retrieve image - 13/2/16
private void getCoImageProfile() {
String url = ClinicOwnerGetImage.DATA_URL+receiveSpEmail.toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showImageJSON(response);
bitmap = getBitmapFromUrl(imagePath);
imageViewCoProfilePageClinicImage.setImageBitmap(bitmap);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ClinicOwnerProfilePage.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showImageJSON(String response){
String image="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(ClinicOwnerGetImage.JSON_ARRAY);
JSONObject employeeData = result.getJSONObject(0);
image = employeeData.getString(ClinicOwnerGetImage.KEY_CLINIC_IMAGE);
} catch (JSONException e) {
e.printStackTrace();
}
imagePath = image;
// added for refreshing event - 5/3
if(swipeRefreshLayout.isRefreshing())
{
swipeRefreshLayout.setRefreshing(false);
}
// end of refreshing
}
public Bitmap getBitmapFromUrl(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//end of retrieve image*/
//new image getter method - 12/3
private void getImage() {
String email = receiveSpEmail.toString();
class GetImage extends AsyncTask<String,Void,Bitmap> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ClinicOwnerProfilePage.this,"Please wait...","Fetching Image...", true, true);
}
@Override
protected void onPostExecute(Bitmap b) {
super.onPostExecute(b);
loading.dismiss();
imageViewCoProfilePageClinicImage.setImageBitmap(b);
// added for refreshing event - 5/3
if(swipeRefreshLayout.isRefreshing())
{
swipeRefreshLayout.setRefreshing(false);
}
// end of refreshing
}
@Override
protected Bitmap doInBackground(String... params) {
String receiveSpEmail = params[0];
String add = "http://cliniclocum.org/CoGetImageProfile.php?email="+receiveSpEmail;
URL url = null;
Bitmap image = null;
try {
url = new URL(add);
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
}
GetImage gi = new GetImage();
gi.execute(email);
}
//end of new method
public void saveSpCoProfile(){
SharedPreferences preferences = getSharedPreferences("PassingCoProfile", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("passCoAddress", editTextCoProfilePageAddress.getText().toString());
editor.putString("passCoClinicName", editTextCoProfilePageName.getText().toString());
editor.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnCoProfileUploadImage:
Intent CoUploadImage = new Intent(this, ClinicOwnerUploadImagePage.class);
startActivity(CoUploadImage);
break;
case R.id.btnCoProfileUpdateProfile:
if(editTextCoProfilePageName.getText().toString().equals("")|| editTextCoProfilePageAddress.getText().toString().equals("")){
Toast.makeText(this, "Please Enter Clinic Name and Address", Toast.LENGTH_LONG).show();
}
else {
String type = "CoUpdate";
String update_CoName = editTextCoProfilePageName.getText().toString();
String update_CoAddress = editTextCoProfilePageAddress.getText().toString();
String update_CoClinicOwnerName = editTextCoProfilePageClinicOwnerName.getText().toString();
String update_CoFacilities = editTextCoProfileFacilities.getText().toString();
String update_CoPatientTurnover = passedSpPatientTurnover;
CoUpdateBackgroundWorker coUpdateBackgroundWorker = new CoUpdateBackgroundWorker(this);
coUpdateBackgroundWorker.execute(type, receiveSpEmail, update_CoName, update_CoAddress, update_CoClinicOwnerName, update_CoFacilities, update_CoPatientTurnover);
coUpdateBackgroundWorker.setOnTaskFinishedListener(new CoUpdateBackgroundWorker.OnTaskFinishedListener() {
@Override
public void onTaskFinished(String result) {
switch (result) {
case "Profile Updated":
saveSpCoProfile();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
Intent successIntent = new Intent(ClinicOwnerProfilePage.this, ClinicOwnerMainPage.class);
startActivity(successIntent);
break;
default:
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
break;
}
}
});
}
break;
}
}
@Override
public void onBackPressed() {
Intent backIntent = new Intent(ClinicOwnerProfilePage.this, ClinicOwnerMainPage.class);
startActivity(backIntent);
}
}
答案 0 :(得分:0)
请检查您的互联网权限 menifests 文件中这样: -
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.example.XYZ.AppName.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.XYZ.AppName.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/cricon"
android:label="@string/activity_logoN"
android:supportsRtl="true"
android:largeHeap="true"
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize"
android:theme="@style/AppTheme">