|
| 1 | +package com.mobtexting; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.pm.ApplicationInfo; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.util.Log; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.lang.annotation.Annotation; |
| 11 | + |
| 12 | +import okhttp3.OkHttpClient; |
| 13 | +import okhttp3.ResponseBody; |
| 14 | +import okhttp3.logging.HttpLoggingInterceptor; |
| 15 | +import retrofit2.Call; |
| 16 | +import retrofit2.Callback; |
| 17 | +import retrofit2.Converter; |
| 18 | +import retrofit2.Response; |
| 19 | +import retrofit2.Retrofit; |
| 20 | +import retrofit2.converter.gson.GsonConverterFactory; |
| 21 | + |
| 22 | + |
| 23 | +public class Mobtexting { |
| 24 | + private static final String TAG = "Communicator"; |
| 25 | + private static final String SERVER_URL = "http://api.mobtexting.com"; |
| 26 | + private Retrofit retrofit; |
| 27 | + private String api_key1; |
| 28 | + private Context context; |
| 29 | + |
| 30 | + public Mobtexting(Context context){ |
| 31 | + this.context=context; |
| 32 | + } |
| 33 | + |
| 34 | + public void sendSMS(String message, String mobile_no, String senderId, final MobtextingInterface mobtextingInterface) { |
| 35 | + try { |
| 36 | + ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); |
| 37 | + Bundle bundle = ai.metaData; |
| 38 | + api_key1= bundle.getString("mobtexting.api_key"); |
| 39 | + }catch (Exception e){ |
| 40 | + mobtextingInterface.onError(new ModelError("100", "API key", "Dear developer. Don't forget to configure <meta-data android:name=\"mobtexting.api_key\" android:value=\"testValue\"/> in your AndroidManifest.xml file.")); |
| 41 | + return; |
| 42 | + } |
| 43 | + if(api_key1!=null&&!api_key1.equals("")) { |
| 44 | + |
| 45 | + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); |
| 46 | + logging.setLevel(HttpLoggingInterceptor.Level.BODY); |
| 47 | + OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); |
| 48 | + httpClient.addInterceptor(logging); |
| 49 | + |
| 50 | + retrofit = new Retrofit.Builder() |
| 51 | + .client(httpClient.build()) |
| 52 | + .addConverterFactory(GsonConverterFactory.create()) |
| 53 | + .baseUrl(SERVER_URL) |
| 54 | + .build(); |
| 55 | + |
| 56 | + Interface service = retrofit.create(Interface.class); |
| 57 | + |
| 58 | + Call<ServerResponse> call = service.post(api_key1, message, mobile_no, senderId); |
| 59 | + |
| 60 | + call.enqueue(new Callback<ServerResponse>() { |
| 61 | + @Override |
| 62 | + public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) { |
| 63 | + // response.isSuccessful() is true if the response code is 2xx |
| 64 | + if (response.isSuccessful()) { |
| 65 | + Log.e(TAG, response.body().toString()); |
| 66 | + mobtextingInterface.onResponse(response.body()); |
| 67 | + } else { |
| 68 | + try { |
| 69 | + Converter<ResponseBody, ModelError> errorConverter = retrofit.responseBodyConverter(ModelError.class, new Annotation[0]); |
| 70 | + ModelError error = errorConverter.convert(response.errorBody()); |
| 71 | + mobtextingInterface.onError(error); |
| 72 | + } catch (IOException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + mobtextingInterface.onError(new ModelError("500", "Something Went Wrong!", "Check your internet connection!/parsing Json exception")); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onFailure(Call<ServerResponse> call, Throwable t) { |
| 81 | + mobtextingInterface.onError(new ModelError("500", "No Internet", "Check your internet connection!")); |
| 82 | + } |
| 83 | + }); |
| 84 | + }else{ |
| 85 | + mobtextingInterface.onError(new ModelError("100", "API key", "Dear developer. Don't forget to configure <meta-data android:name=\"mobtexting.api_key\" android:value=\"testValue\"/> in your AndroidManifest.xml file.")); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /*public void loginGet(String username, String password) { |
| 90 | +
|
| 91 | + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); |
| 92 | + logging.setLevel(HttpLoggingInterceptor.Level.BODY); |
| 93 | + OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); |
| 94 | + httpClient.addInterceptor(logging); |
| 95 | +
|
| 96 | + Retrofit retrofit = new Retrofit.Builder() |
| 97 | + .client(httpClient.build()) |
| 98 | + .addConverterFactory(GsonConverterFactory.create()) |
| 99 | + .baseUrl(SERVER_URL) |
| 100 | + .build(); |
| 101 | +
|
| 102 | + Interface service = retrofit.create(Interface.class); |
| 103 | +
|
| 104 | + Call<ServerResponse> call = service.get("login", username, password); |
| 105 | +
|
| 106 | + call.enqueue(new Callback<ServerResponse>() { |
| 107 | + @Override |
| 108 | + public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) { |
| 109 | + // response.isSuccessful() is true if the response code is 2xx |
| 110 | + Log.e(TAG, "Success"); |
| 111 | + } |
| 112 | +
|
| 113 | + @Override |
| 114 | + public void onFailure(Call<ServerResponse> call, Throwable t) { |
| 115 | + // handle execution failures like no internet connectivity |
| 116 | + Log.d("internet",t.getMessage()); |
| 117 | + } |
| 118 | + }); |
| 119 | + }*/ |
| 120 | + |
| 121 | + |
| 122 | +} |
| 123 | + |
0 commit comments