mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Include feature to check server connections
This commit is contained in:
		
							parent
							
								
									e6640d395b
								
							
						
					
					
						commit
						ede34920c2
					
				| @ -34,17 +34,20 @@ | |||||||
|         android:layout_alignParentEnd="true" |         android:layout_alignParentEnd="true" | ||||||
|         android:layout_alignParentRight="true" |         android:layout_alignParentRight="true" | ||||||
|         android:layout_alignParentTop="true" |         android:layout_alignParentTop="true" | ||||||
|  |         android:enabled="false" | ||||||
|         android:onClick="onClickHandler" |         android:onClick="onClickHandler" | ||||||
|         android:text="@string/refresh"/> |         android:text="@string/refresh" | ||||||
|  |         android:visibility="invisible"/> | ||||||
| 
 | 
 | ||||||
|     <Button |     <Button | ||||||
|         android:id="@+id/button_check_server" |         android:id="@+id/button_check_server" | ||||||
|         style="@style/ButtonStyle" |         style="@style/ButtonStyle" | ||||||
|         android:layout_width="wrap_content" |         android:layout_width="wrap_content" | ||||||
|         android:layout_height="wrap_content" |         android:layout_height="wrap_content" | ||||||
|         android:layout_alignParentLeft="true" |         android:layout_alignBaseline="@+id/server_address" | ||||||
|         android:layout_alignParentStart="true" |         android:layout_alignBottom="@+id/server_address" | ||||||
|         android:layout_alignParentTop="true" |         android:layout_alignParentEnd="true" | ||||||
|  |         android:layout_alignParentRight="true" | ||||||
|         android:onClick="onClickHandler" |         android:onClick="onClickHandler" | ||||||
|         android:text="@string/check_server"/> |         android:text="@string/check_server"/> | ||||||
| 
 | 
 | ||||||
| @ -52,42 +55,45 @@ | |||||||
|         android:id="@+id/list_view" |         android:id="@+id/list_view" | ||||||
|         android:layout_width="match_parent" |         android:layout_width="match_parent" | ||||||
|         android:layout_height="wrap_content" |         android:layout_height="wrap_content" | ||||||
| 		android:layout_below="@+id/button_refresh"  |  | ||||||
|         android:layout_above="@+id/button_upload" |         android:layout_above="@+id/button_upload" | ||||||
|         android:layout_alignParentLeft="true" |         android:layout_alignParentLeft="true" | ||||||
|         android:layout_alignParentRight="true" |         android:layout_alignParentRight="true" | ||||||
|         > |         android:layout_below="@+id/button_refresh" | ||||||
|  |         android:visibility="invisible"> | ||||||
|     </ListView> |     </ListView> | ||||||
| 
 | 
 | ||||||
|     <Button |     <Button | ||||||
|         android:id="@+id/button_upload" |         android:id="@+id/button_upload" | ||||||
|         style="@style/ButtonStyle" |         style="@style/ButtonStyle" | ||||||
|         android:layout_alignParentLeft="true" |  | ||||||
|         android:layout_above="@+id/frame" |         android:layout_above="@+id/frame" | ||||||
|         android:text="@string/upload" |         android:layout_alignParentLeft="true" | ||||||
|  |         android:enabled="false" | ||||||
|         android:onClick="onClickHandler" |         android:onClick="onClickHandler" | ||||||
|         /> |         android:text="@string/upload" | ||||||
|  |         android:visibility="invisible"/> | ||||||
| 
 | 
 | ||||||
|     <TextView |     <TextView | ||||||
|         android:id="@+id/upload_progress" |         android:id="@+id/upload_progress" | ||||||
|         style="@style/ProgressStyle" |         style="@style/ProgressStyle" | ||||||
|         android:layout_below="@id/list_view" |  | ||||||
|         android:layout_above="@id/frame" |         android:layout_above="@id/frame" | ||||||
|         android:layout_toRightOf="@id/button_upload" |         android:layout_below="@id/list_view" | ||||||
|         android:layout_toLeftOf="@+id/button_delete_remote" |         android:layout_toLeftOf="@+id/button_delete_remote" | ||||||
|  |         android:layout_toRightOf="@id/button_upload" | ||||||
|  |         android:enabled="false" | ||||||
|         android:gravity="center" |         android:gravity="center" | ||||||
|         android:textSize="14sp" |  | ||||||
|         android:text="0%" |         android:text="0%" | ||||||
|         /> |         android:textSize="14sp" | ||||||
|  |         android:visibility="invisible"/> | ||||||
| 
 | 
 | ||||||
|     <Button |     <Button | ||||||
|         android:id="@id/button_delete_remote" |         android:id="@id/button_delete_remote" | ||||||
|         style="@style/ButtonStyle" |         style="@style/ButtonStyle" | ||||||
|         android:layout_alignParentRight="true" |  | ||||||
|         android:layout_above="@id/frame" |         android:layout_above="@id/frame" | ||||||
|         android:text="@string/delete_remote_file"  |         android:layout_alignParentRight="true" | ||||||
|  |         android:enabled="false" | ||||||
|         android:onClick="onClickHandler" |         android:onClick="onClickHandler" | ||||||
|         /> |         android:text="@string/delete_remote_file" | ||||||
|  |         android:visibility="invisible"/> | ||||||
| 
 | 
 | ||||||
|     <FrameLayout |     <FrameLayout | ||||||
|         android:id="@id/frame" |         android:id="@id/frame" | ||||||
| @ -104,30 +110,46 @@ | |||||||
|         style="@style/ButtonStyle" |         style="@style/ButtonStyle" | ||||||
|         android:layout_alignParentBottom="true" |         android:layout_alignParentBottom="true" | ||||||
|         android:layout_alignParentLeft="true" |         android:layout_alignParentLeft="true" | ||||||
|         android:text="@string/download"  |         android:enabled="false" | ||||||
|         android:onClick="onClickHandler" |         android:onClick="onClickHandler" | ||||||
|         /> |         android:text="@string/download" | ||||||
|  |         android:visibility="invisible"/> | ||||||
| 
 | 
 | ||||||
|     <TextView |     <TextView | ||||||
|         android:id="@+id/download_progress" |         android:id="@+id/download_progress" | ||||||
|         style="@style/ProgressStyle" |         style="@style/ProgressStyle" | ||||||
|         android:layout_below="@id/frame" |  | ||||||
|         android:layout_alignParentBottom="true" |         android:layout_alignParentBottom="true" | ||||||
|         android:layout_toRightOf="@id/button_download" |         android:layout_below="@id/frame" | ||||||
|         android:layout_toLeftOf="@+id/button_delete_local" |         android:layout_toLeftOf="@+id/button_delete_local" | ||||||
|  |         android:layout_toRightOf="@id/button_download" | ||||||
|  |         android:enabled="false" | ||||||
|         android:gravity="center" |         android:gravity="center" | ||||||
|         android:textSize="14sp" |  | ||||||
|         android:text="0%" |         android:text="0%" | ||||||
|         /> |         android:textSize="14sp" | ||||||
|  |         android:visibility="invisible"/> | ||||||
| 
 | 
 | ||||||
|     <Button |     <Button | ||||||
|         android:id="@id/button_delete_local" |         android:id="@id/button_delete_local" | ||||||
|         style="@style/ButtonStyle" |         style="@style/ButtonStyle" | ||||||
|         android:layout_alignParentBottom="true" |         android:layout_alignParentBottom="true" | ||||||
|         android:layout_alignParentRight="true" |         android:layout_alignParentRight="true" | ||||||
|         android:text="@string/delete_local_file"  |         android:enabled="false" | ||||||
|         android:onClick="onClickHandler" |         android:onClick="onClickHandler" | ||||||
|         /> |         android:text="@string/delete_local_file" | ||||||
|  |         android:visibility="invisible"/> | ||||||
|  | 
 | ||||||
|  |     <EditText | ||||||
|  |         android:id="@+id/server_address" | ||||||
|  |         android:layout_width="wrap_content" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:layout_above="@+id/list_view" | ||||||
|  |         android:layout_alignParentLeft="true" | ||||||
|  |         android:layout_alignParentStart="true" | ||||||
|  |         android:layout_toLeftOf="@+id/button_check_server" | ||||||
|  |         android:layout_toStartOf="@+id/button_check_server" | ||||||
|  |         android:ems="10" | ||||||
|  |         android:hint="Server address (with http or https)" | ||||||
|  |         android:inputType="textPersonName"/> | ||||||
| 
 | 
 | ||||||
| </RelativeLayout> | </RelativeLayout> | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -31,6 +31,7 @@ import android.net.Uri; | |||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
| import android.os.Handler; | import android.os.Handler; | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
|  | import android.view.Gravity; | ||||||
| import android.view.View; | import android.view.View; | ||||||
| import android.widget.ListView; | import android.widget.ListView; | ||||||
| import android.widget.TextView; | import android.widget.TextView; | ||||||
| @ -50,6 +51,9 @@ import com.owncloud.android.lib.resources.files.RemoteFile; | |||||||
| import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; | import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; | ||||||
| import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; | import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; | ||||||
| 
 | 
 | ||||||
|  | import org.json.JSONException; | ||||||
|  | import org.json.JSONObject; | ||||||
|  | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| @ -78,6 +82,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, | |||||||
| 	 | 	 | ||||||
| 	private View mFrame; | 	private View mFrame; | ||||||
| 
 | 
 | ||||||
|  | 	private static final String NODE_VERSION = "version"; | ||||||
|  | 	 | ||||||
|     /** Called when the activity is first created. */ |     /** Called when the activity is first created. */ | ||||||
|     @Override |     @Override | ||||||
|     public void onCreate(Bundle savedInstanceState) { |     public void onCreate(Bundle savedInstanceState) { | ||||||
| @ -160,27 +166,93 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, | |||||||
| 
 | 
 | ||||||
|     private void startCheck() { |     private void startCheck() { | ||||||
| 
 | 
 | ||||||
|  |     	String serverAddress = ((TextView) findViewById(R.id.server_address)).getText().toString(); | ||||||
|  | 
 | ||||||
|  |     	if (serverAddress.equals("")) { | ||||||
|  | 			Toast toast = Toast.makeText(getApplicationContext(), | ||||||
|  | 					"Introduce a server address", Toast.LENGTH_LONG); | ||||||
|  | 
 | ||||||
|  | 			toast.setGravity(Gravity.CENTER,0,0); | ||||||
|  | 
 | ||||||
|  | 			toast.show(); | ||||||
|  | 
 | ||||||
|  | 			return; | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		OkHttpClient client = new OkHttpClient(); | 		OkHttpClient client = new OkHttpClient(); | ||||||
| 
 | 
 | ||||||
| 		Request request = new Request.Builder() | 		Request request = new Request.Builder() | ||||||
| 				.url("") | 				.url(serverAddress + "/status.php") | ||||||
| 				.get() | 				.get() | ||||||
| 				.build(); | 				.build(); | ||||||
| 
 | 
 | ||||||
| 		client.newCall(request).enqueue(new Callback() { | 		client.newCall(request).enqueue(new Callback() { | ||||||
| 			@Override public void onFailure(Call call, IOException e) { | 
 | ||||||
|  | 			@Override public void onFailure(Call call, final IOException e) { | ||||||
|  | 				runOnUiThread(new Runnable() { | ||||||
|  | 					@Override | ||||||
|  | 					public void run() { | ||||||
|  | 
 | ||||||
|  | 						Toast toast = Toast.makeText(getApplicationContext(), | ||||||
|  | 								"Request failed: " + e.toString(), Toast.LENGTH_LONG); | ||||||
|  | 
 | ||||||
|  | 						toast.setGravity(Gravity.CENTER,0,0); | ||||||
|  | 
 | ||||||
|  | 						toast.show(); | ||||||
|  | 					} | ||||||
|  | 				}); | ||||||
| 				e.printStackTrace(); | 				e.printStackTrace(); | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			@Override public void onResponse(Call call, Response response) throws IOException { | 			@Override public void onResponse(Call call, final Response response) throws IOException { | ||||||
| 				if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); | 
 | ||||||
|  | 				if (!response.isSuccessful()) { | ||||||
|  | 
 | ||||||
|  | 					runOnUiThread(new Runnable() { | ||||||
|  | 						@Override | ||||||
|  | 						public void run() { | ||||||
|  | 
 | ||||||
|  | 							Toast toast = Toast.makeText(getApplicationContext(), | ||||||
|  | 									"Response not successful with code " + response.code(), Toast.LENGTH_LONG); | ||||||
|  | 
 | ||||||
|  | 							toast.setGravity(Gravity.CENTER,0,0); | ||||||
|  | 
 | ||||||
|  | 							toast.show(); | ||||||
|  | 						} | ||||||
|  | 					}); | ||||||
|  | 
 | ||||||
|  | 					throw new IOException("Unexpected code " + response); | ||||||
|  | 				} | ||||||
|  | 
 | ||||||
|  | 				try { // Response successful | ||||||
|  | 
 | ||||||
|  | 					String jsonData = response.body().string(); | ||||||
|  | 
 | ||||||
|  | 					JSONObject Jobject = new JSONObject(jsonData); | ||||||
|  | 
 | ||||||
|  | 					final String serverVersion = Jobject.get("version").toString(); | ||||||
|  | 
 | ||||||
|  | 					runOnUiThread(new Runnable() { | ||||||
|  | 						@Override | ||||||
|  | 						public void run() { | ||||||
|  | 
 | ||||||
|  | 							Toast toast = Toast.makeText(getApplicationContext(), | ||||||
|  | 									"Server with version " + serverVersion + " detected", Toast.LENGTH_LONG); | ||||||
|  | 
 | ||||||
|  | 							toast.setGravity(Gravity.CENTER,0,0); | ||||||
|  | 
 | ||||||
|  | 							toast.show(); | ||||||
|  | 						} | ||||||
|  | 					}); | ||||||
|  | 
 | ||||||
|  | 				} catch (JSONException e) { | ||||||
|  | 					e.printStackTrace(); | ||||||
|  | 				} | ||||||
| 
 | 
 | ||||||
| 				Headers responseHeaders = response.headers(); | 				Headers responseHeaders = response.headers(); | ||||||
| 				for (int i = 0, size = responseHeaders.size(); i < size; i++) { | 				for (int i = 0, size = responseHeaders.size(); i < size; i++) { | ||||||
| 					System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); | 					System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); | ||||||
| 				} | 				} | ||||||
| 
 |  | ||||||
| 				System.out.println(response.body().string()); |  | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
| @ -205,6 +277,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, | |||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			@Override public void onResponse(Call call, Response response) throws IOException { | 			@Override public void onResponse(Call call, Response response) throws IOException { | ||||||
|  | 
 | ||||||
| 				if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); | 				if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); | ||||||
| 
 | 
 | ||||||
| 				Headers responseHeaders = response.headers(); | 				Headers responseHeaders = response.headers(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user