Created
August 15, 2017 21:28
-
-
Save minakhan01/6f23add576b3df81b61b4c1cd96c082a to your computer and use it in GitHub Desktop.
network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cafe.adriel.androidaudiorecorder.example; | |
import android.Manifest; | |
import android.content.Intent; | |
import android.graphics.drawable.ColorDrawable; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.NetworkResponse; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.ServerError; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.HttpHeaderParser; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.UnsupportedEncodingException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import cafe.adriel.androidaudiorecorder.AndroidAudioRecorder; | |
import cafe.adriel.androidaudiorecorder.model.AudioChannel; | |
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate; | |
import cafe.adriel.androidaudiorecorder.model.AudioSource; | |
public class MainActivity extends AppCompatActivity { | |
private static final int REQUEST_RECORD_AUDIO = 0; | |
private static final String AUDIO_FILE_PATH = | |
Environment.getExternalStorageDirectory().getPath() + "/recorded_audio.wav"; | |
RequestQueue queue; | |
Button sendButton; | |
EditText textField; | |
//View.OnClickListener sendButtonListener; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if (getSupportActionBar() != null) { | |
getSupportActionBar().setBackgroundDrawable( | |
new ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimaryDark))); | |
} | |
Util.requestPermission(this, Manifest.permission.RECORD_AUDIO); | |
Util.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
textField = (EditText) findViewById(R.id.text_field); | |
sendButton = (Button) findViewById(R.id.send_button) ; | |
sendButton.setOnClickListener(sendButtonListener); | |
queue = Volley.newRequestQueue(this); | |
//simpleRequest(); | |
stringRequest(); | |
jsonRequest(); | |
voiceRequest(); | |
} | |
protected View.OnClickListener sendButtonListener = new View.OnClickListener(){ | |
public void onClick(View v) { | |
Log.d("HELLO", "send button pressed"); | |
String textMessage = textField.getText().toString(); | |
sendMessage(textMessage); | |
} | |
}; | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == REQUEST_RECORD_AUDIO) { | |
if (resultCode == RESULT_OK) { | |
Toast.makeText(this, "Audio recorded successfully!", Toast.LENGTH_SHORT).show(); | |
} else if (resultCode == RESULT_CANCELED) { | |
Toast.makeText(this, "Audio was not recorded", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
public void recordAudio(View v) { | |
AndroidAudioRecorder.with(this) | |
// Required | |
.setFilePath(AUDIO_FILE_PATH) | |
.setColor(ContextCompat.getColor(this, R.color.recorder_bg)) | |
.setRequestCode(REQUEST_RECORD_AUDIO) | |
// Optional | |
.setSource(AudioSource.MIC) | |
.setChannel(AudioChannel.STEREO) | |
.setSampleRate(AudioSampleRate.HZ_48000) | |
.setAutoStart(false) | |
.setKeepDisplayOn(true) | |
// Start recording | |
.record(); | |
} | |
private void simpleRequest() { | |
// Instantiate the RequestQueue. | |
String url ="http://www.google.com"; | |
// Request a string response from the provided URL. | |
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, | |
new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
// Display the first 500 characters of the response string. | |
Log.d("HELLO", "Response is: "+ response.substring(0,500)); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Log.d("HELLO", "That didn't work!"); | |
} | |
}); | |
// Add the request to the RequestQueue. | |
queue.add(stringRequest); | |
} | |
private void headerRequest() { | |
String url= "https://iceus.azure-api.net/intern/event/api/chat"; | |
RequestQueue queue = Volley.newRequestQueue(this); | |
StringRequest postRequest = new StringRequest(Request.Method.GET, url, | |
new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse(String response) { | |
// response | |
Log.d("Response", response); | |
} | |
}, | |
new Response.ErrorListener() | |
{ | |
@Override | |
/* import com.android.volley.toolbox.HttpHeaderParser; */ | |
public void onErrorResponse(VolleyError error) { | |
// As of f605da3 the following should work | |
NetworkResponse response = error.networkResponse; | |
if (error instanceof ServerError && response != null) { | |
try { | |
String res = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers, "utf-8")); | |
// Now you can use any deserializer to make sense of data | |
JSONObject obj = new JSONObject(res); | |
} catch (UnsupportedEncodingException e1) { | |
// Couldn't properly decode data to string | |
e1.printStackTrace(); | |
} catch (JSONException e2) { | |
// returned data is not JSONObject? | |
e2.printStackTrace(); | |
} | |
} | |
} | |
} | |
) { | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("Content-Type", "application/json; charset=utf-8"); | |
params.put("Ocp-Apim-Trace", "true"); | |
params.put("Ocp-Apim-Subscription-Key", "94fee08703ef40909632ec84a1104df8"); | |
return params; | |
} | |
}; | |
queue.add(postRequest); | |
} | |
private void stringRequest() { | |
String url= "https://" + | |
"iceus.azure-api.net/intern/event/api/chat"; | |
StringRequest postRequest = new StringRequest(Request.Method.POST, url, | |
new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse(String response) { | |
// response | |
Log.d("Response", response); | |
} | |
}, | |
new Response.ErrorListener() | |
{ | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
// As of f605da3 the following should work | |
NetworkResponse response = error.networkResponse; | |
if (error instanceof ServerError && response != null) { | |
try { | |
String res = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers, "utf-8")); | |
// Now you can use any deserializer to make sense of data | |
JSONObject obj = new JSONObject("{"+res+"}"); | |
} catch (UnsupportedEncodingException e1) { | |
// Couldn't properly decode data to string | |
e1.printStackTrace(); | |
} catch (JSONException e2) { | |
// returned data is not JSONObject? | |
e2.printStackTrace(); | |
} | |
} | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() | |
{ | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("TextMessage", "rhyme with me Zo"); | |
params.put("UserId", "arsTechnicaDemoUser"); | |
params.put("ClientId", "facebook"); | |
params.put("IsVoice", "true"); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> params = new HashMap<String, String>(); | |
//params.put("Content-Type", "application/json"); | |
params.put("Ocp-Apim-Trace", "true"); | |
params.put("Ocp-Apim-Subscription-Key", "94fee08703ef40909632ec84a1104df8"); | |
return params; | |
} | |
}; | |
queue.add(postRequest); | |
} | |
private void voiceRequestJson() { | |
String url= "https://iceus.azure-api.net/intern/event/api/voice"; | |
StringRequest postRequest = new StringRequest(Request.Method.GET, url, | |
new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse(String response) { | |
// response | |
Log.d("Response", response); | |
} | |
}, | |
new Response.ErrorListener() | |
{ | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
// As of f605da3 the following should work | |
NetworkResponse response = error.networkResponse; | |
if (error instanceof ServerError && response != null) { | |
try { | |
String res = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers, "utf-8")); | |
// Now you can use any deserializer to make sense of data | |
JSONObject obj = new JSONObject("{"+res+"}"); | |
} catch (UnsupportedEncodingException e1) { | |
// Couldn't properly decode data to string | |
e1.printStackTrace(); | |
} catch (JSONException e2) { | |
// returned data is not JSONObject? | |
e2.printStackTrace(); | |
} | |
} | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() | |
{ | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("text", "rhyme with me Zo"); | |
params.put("outputFormat", "audio-16khz-32kbitrate-mono-mp3"); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> params = new HashMap<String, String>(); | |
//params.put("Content-Type", "application/json"); | |
params.put("Ocp-Apim-Trace", "true"); | |
params.put("Ocp-Apim-Subscription-Key", "94fee08703ef40909632ec84a1104df8"); | |
return params; | |
} | |
}; | |
queue.add(postRequest); | |
} | |
private void voiceRequest() { | |
String url= "https://iceus.azure-api.net/intern/event/api/voice"; | |
StringRequest postRequest = new StringRequest(Request.Method.GET, url, | |
new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse(String response) { | |
// response | |
Log.d("Response", response); | |
} | |
}, | |
new Response.ErrorListener() | |
{ | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
// As of f605da3 the following should work | |
NetworkResponse response = error.networkResponse; | |
if (error instanceof ServerError && response != null) { | |
try { | |
String res = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers, "utf-8")); | |
// Now you can use any deserializer to make sense of data | |
//JSONObject obj = new JSONObject("{"+res+"}"); | |
} catch (UnsupportedEncodingException e1) { | |
// Couldn't properly decode data to string | |
e1.printStackTrace(); | |
} catch (JSONException e2) { | |
// returned data is not JSONObject? | |
e2.printStackTrace(); | |
} | |
} | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() | |
{ | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("text", "rhyme with me Zo"); | |
params.put("outputFormat", "audio-16khz-32kbitrate-mono-mp3"); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> params = new HashMap<String, String>(); | |
//params.put("Content-Type", "application/json"); | |
params.put("Ocp-Apim-Trace", "true"); | |
params.put("Ocp-Apim-Subscription-Key", "94fee08703ef40909632ec84a1104df8"); | |
return params; | |
} | |
}; | |
queue.add(postRequest); | |
} | |
private void jsonRequest() { | |
String url= "https://" + | |
"iceus.azure-api.net/intern/event/api/chat"; | |
StringRequest postRequest = new StringRequest(Request.Method.POST, url, | |
new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse(String response) { | |
// response | |
Log.d("Response", response); | |
} | |
}, | |
new Response.ErrorListener() | |
{ | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
// As of f605da3 the following should work | |
NetworkResponse response = error.networkResponse; | |
if (error instanceof ServerError && response != null) { | |
try { | |
String res = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers, "utf-8")); | |
// Now you can use any deserializer to make sense of data | |
JSONObject obj = new JSONObject("{"+res+"}"); | |
obj.getString("TextReply"); | |
JSONArray delayedReplies = obj.getJSONArray("DelayedResponses"); | |
for (int i = 0; i < delayedReplies.length(); i++) { | |
JSONObject delayedReply = delayedReplies.getJSONObject(i); | |
JSONArray textReplies = delayedReply.getJSONArray("TextReplies"); | |
for (int j = 0; j < textReplies.length(); j++) { | |
JSONObject textReply = textReplies.getJSONObject(j); | |
String reply = textReply.getString("TextReplies"); | |
Log.d("ZO", "reply: "+reply); | |
} | |
} | |
} catch (UnsupportedEncodingException e1) { | |
// Couldn't properly decode data to string | |
e1.printStackTrace(); | |
} catch (JSONException e2) { | |
// returned data is not JSONObject? | |
e2.printStackTrace(); | |
} | |
} | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() | |
{ | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("TextMessage", "rhyme with me Zo"); | |
params.put("UserId", "arsTechnicaDemoUser"); | |
params.put("ClientId", "facebook"); | |
params.put("IsVoice", "true"); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> params = new HashMap<String, String>(); | |
//params.put("Content-Type", "application/json"); | |
params.put("Ocp-Apim-Trace", "true"); | |
params.put("Ocp-Apim-Subscription-Key", "94fee08703ef40909632ec84a1104df8"); | |
return params; | |
} | |
}; | |
queue.add(postRequest); | |
} | |
private void sendMessage(final String message) { | |
String url= "https://" + | |
"iceus.azure-api.net/intern/event/api/chat"; | |
StringRequest postRequest = new StringRequest(Request.Method.POST, url, | |
new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse(String response) { | |
// response | |
Log.d("Response", response); | |
} | |
}, | |
new Response.ErrorListener() | |
{ | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
// As of f605da3 the following should work | |
NetworkResponse response = error.networkResponse; | |
if (error instanceof ServerError && response != null) { | |
try { | |
String res = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers, "utf-8")); | |
// Now you can use any deserializer to make sense of data | |
JSONObject obj = new JSONObject("{"+res+"}"); | |
} catch (UnsupportedEncodingException e1) { | |
// Couldn't properly decode data to string | |
e1.printStackTrace(); | |
} catch (JSONException e2) { | |
// returned data is not JSONObject? | |
e2.printStackTrace(); | |
} | |
} | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() | |
{ | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("TextMessage", message); | |
params.put("UserId", "arsTechnicaDemoUser"); | |
params.put("ClientId", "facebook"); | |
params.put("IsVoice", "true"); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> params = new HashMap<String, String>(); | |
//params.put("Content-Type", "application/json"); | |
params.put("Ocp-Apim-Trace", "true"); | |
params.put("Ocp-Apim-Subscription-Key", "94fee08703ef40909632ec84a1104df8"); | |
return params; | |
} | |
}; | |
queue.add(postRequest); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment